0

I bet there must be some plugin out there which might turn this problem into something way easier to solve, like MomentJs but the truth is; I don't know how to use it :) So I want to do it as basic as possible.

I've built an ALMOST-WORKING example of what I want to achieve here:

http://jsfiddle.net/Frondor/5a5wrssn/

The interface and controls are made, but I'm still missing the functions to: 1. Automatically detect user's timezone (like '-1', '0', '+2') and calculate the given time (see input #time) to match the french timezone (which is UTC/GMT +2).

In some kind of order:

  1. Get user current time/timezone (hours only, not minutes or seconds)
  2. Get the time introduced by the user to calculate Calculate
  3. introduced time with other country's time (France in this case).
  4. Return the value as hours like 02:00

This is my script so far, these are controls mostly, I don't know how to build the needed functions for this to work like I described above :)

$(document).ready(function () {
//Automatically detect user's timezone and store it inside this variable
    //Let's use a temporal example just to see how the whole script works
    var userTimeZone = "-3"; //here goes a function, its just an example of the variable userTimeZone already resolved for some function I don't know how to code hehe

    $("#detected").hide().fadeIn(2000); //just4fancy
    $("#uTZ").text(userTimeZone); //print the detected timezone

    var timeInput = $('#time');
    timeInput.focus();
    timeInput.on('change keyup mouseup', function () {
        var userTZ = $("#uTZ").text();

        var franceTZ = "2"; //predefined timezone of France +2

//Now I try to set the differnce of timezones in order to see how to calculate the final time        
        var calculation = userTZ-franceTZ; //here is where the calculation happens. Actually, It is (of course) wrong :)

        var actualValue = $(this).val();

//print the results
        if (timeInput.val() !== '00') {
            $('#actual').text(actualValue + ':00 hours');

            $('#result').text(actualValue - calculation + ':00 hours'); //this calculates
the given time with the difference of timezones to show a converted time

            $('.panel-footer').removeClass('hidden').hide().slideDown();
        }
    }).bind('mousewheel', function (e) {
        return false;
    });
});

If its useful for somebody, I've found this Convert timezone offset number of hours to timezone offset in (military?) hours but I didn't know how to implement the same on my script :(

Thanks in advance!

2
  • Try moment.js Commented Sep 27, 2014 at 12:30
  • I encourage you to read what I wrote, specially the first paragraph.
    – Frondor
    Commented Sep 27, 2014 at 12:35

2 Answers 2

4

A few things:

  • A time zone is not the same as an offset, and cannot be treated as such. Nor can offsets be limited to whole hours. Please read "Time Zone != Offset" in the timezone tag wiki.

  • France's time zone is "Europe/Paris". It uses the +2 offset only in the summer months when daylight saving time is in effect. The rest of the year, it uses a +1 offset.

  • You don't actually need to detect the user's time zone in order to make this work. The browser already works within the user's time zone. You just need to be able to work with French time regardless of the browser setting, which you can do with a library.

  • You said you have used moment.js. You simply need to use the moment-timezone extensions.

    // current time in France
    moment().tz('Europe/Paris').format('H:mm')
    
    // When it's 3:00 local time today, what time is it in France?
    moment('3:00','H:mm').tz('Europe/Paris').format('H:mm')
    
  • You should be careful with the phrasing of how you present the information. It's invalid to simply say "When it's X time in A, it's Y time in B". A date must also be involved, as the answer could change based on whether DST is currently in effect in A or in B. You can reference "today", but make sure you know whether that's today in A or today in B. Those might not be the same.

  • You may also find that some input values are ambiguous. For example, if I say it's 1:00 AM in New York on November 2nd 2014, then that could be 1:00 EDT (UTC-4), or it could be one hour later at 1:00 EST (UTC-5). You will find that given ambiguous input, some browsers will choose the daylight time (IE, Safari), while some will choose the standard time (Chrome, Firefox, Opera, NodeJS).

1
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" >
    /*finding time for india and usa in both 12hr and 24 hr format*/
function indUsaTime(){
    msg1=document.getElementById("msg1");
    msg2=document.getElementById("msg2");
    msg3=document.getElementById("msg3");
    msg4=document.getElementById("msg4");
    dayFormatIndia=document.getElementById("dayFormatIndia");
    dayFormatUs=document.getElementById("dayFormatUs");
        /*creating date object to collect hours/minutes/seconds*/
        var date=new Date();
        var hrs=date.getHours();
            /*converting into 12hr format*/
            if(hrs>12){
                hrs1=hrs%12;
                dayFormatIndia.innerHTML="PM";
            }
            else if(hrs==12){
               hrs1=hrs;
               dayFormatIndia.innerHTML="PM";
              }else{
                hrs1=hrs;
                dayFormatIndia.innerHTML="AM";
               }
    /*printing both 12hr and 24hr format for India*/
    msg1.innerHTML=hrs1+":"+date.getMinutes()+":"+date.getSeconds();
    msg2.innerHTML=hrs+":"+date.getMinutes()+":"+date.getSeconds();
        var hrsUS=hrs-10;
        var mnsUS=date.getMinutes()+30;
        mnsUS=mnsUS%60;
        /*converting hours of usa east cost using its minutes*/
            if(mnsUS>=0&&mnsUS<30){
                hrsUS=date.getHours()-9;
            }else{
                hrsUS=date.getHours()-10;
            }
        /*converting into proper 24 hr format based upon cases*/    
            if(hrsUS<0){
                hrsUS=hrsUS+24;
            }    
            if(hrsUS>24){
                hrsUS=hrsUS%24;
            }
            /*converting into proper 12 hr format based upon cases*/    
            if(hrsUS<0){
                hrsUS=hrsUS+12;
            }else{
                hrsUS=hrsUS;
            }
            if(hrsUS>=12){
                hrsUS1=hrsUS%12;
                dayFormatUs.innerHTML="PM";
            }if(hrsUS==0){
                hrsUS1=hrsUS+12;
                dayFormatUs.innerHTML="AM";
            }else{
                hrsUS1=hrsUS;
                dayFormatUs.innerHTML="AM";
            }
         /*printing both 12hr format and 24hr format*/    
            msg3.innerHTML=hrsUS1+":"+mnsUS+":"+date.getSeconds();
            msg4.innerHTML=hrsUS+":"+mnsUS+":"+date.getSeconds()
        }
 /*calling indUsaTime() in each second sothat it will look dynamic*/        
setInterval(indUsaTime,1000);
function bodyload(){
    indUsaTime();
     /*input countries as an array and appending on bodyload*/
    countries=["select","UK(+1 GMT)","Germany(+2 GMT)","Spain(+2 GMT)","Brazil(-3 GMT)","Mexico(-5 GMT)","Japan(+9 GMT)","Canada(-4 GMT)","Aus(+10 GMT)","Qatar(+3 GMT)"];
        country=document.getElementById("country");
        for(i=0;i<countries.length;i++){
            var opt=document.createElement("option");
            opt.text=countries[i];
            opt.values=countries[i];
            country.appendChild(opt);
        }  
}
 /*finding time for all the countries in our list*/
function findTime(){
    country=document.getElementById("country");
    showTime=document.getElementById("showTime");
    var date=new Date();
       switch(country.value)
       {
           case "UK(+1 GMT)":
               /*converting minute of UK*/
               ukMns=date.getMinutes()+30;
               ukMns=ukMns%60;
                   /*converting hours of UK*/    
                   if(ukMns>=0&&ukMns<30){
                       ukHrs=date.getHours()-4;
                    }else{
                       ukHrs=date.getHours()-5;
                    }
                    if(ukHrs<0){
                       ukHrs=ukHrs+24;
                    }else{
                       ukHrs=ukHrs;
                    }
                /*printing 24hr format*/ 
                showTime.innerHTML=ukHrs+":"+ukMns+":"+date.getSeconds();
            break;
            case "Germany(+2 GMT)":
                /*converting minute of Germany*/
                germanyMns=date.getMinutes()+30;
                germanyMns=germanyMns%60;
                   /*converting hours of Germany*/ 
                    if(germanyMns>=0&&germanyMns<30){
                        germanyHrs=date.getHours()-3;
                    }else{
                        germanyHrs=date.getHours()-4;
                    }
                    if(germanyHrs<0){
                        germanyHrs=germanyHrs+24;
                    }else{
                        germanyHrs=germanyHrs;
                    }
                /*printing 24hr format*/    
                showTime.innerHTML=germanyHrs+":"+germanyMns+":"+date.getSeconds();
            break;
            case "Spain(+2 GMT)":
                 /*converting minutes of Spain*/ 
                spainMns=date.getMinutes()+30;
                spainMns=spainMns%60;
                     /*converting hours of Spain*/ 
                    if(spainMns>=0 && spainMns<30){
                        spainHrs=date.getHours()-3;
                    }else{
                         spainHrs=date.getHours()-4;
                    }
                    if(spainHrs<0){
                        spainHrs=spainHrs+24;
                    }else{
                        spainHrs=spainHrs;
                    }
                /*printing 24hr format*/     
                showTime.innerHTML=spainHrs+":"+spainMns+":"+date.getSeconds();
            break;
            case "Mexico(-5 GMT)":
                 /*converting minutes of Mexico*/            
                mexicoMns=date.getMinutes()+30;
                mexicoMns=mexicoMns%60;
                     /*converting hours of Spain*/ 
                    if(mexicoMns>=0 && mexicoMns<30){
                        mexicohrs=date.getHours()-10;
                    }
                    else{
                        mexicohrs=date.getHours()-11;
                    }
                    if(mexicohrs<0){
                        mexicohrs=mexicohrs+24;
                    }
                    else{
                        mexicohrs=mexicohrs;
                    }
                 /*printing 24hr format*/     
                showTime.innerHTML=mexicohrs+":"+mexicoMns+":"+date.getSeconds();
            break;
            case "Brazil(-3 GMT)":
                /*converting minutes Brazil*/ 
                brazilMns=date.getMinutes()+30;
                brazilMns=brazilMns%60;
                    /*converting hours of Mexico*/ 
                    if(brazilMns>=0 && brazilMns<30){
                        brazilHrs=date.getHours()-8;
                    }else{
                        brazilHrs=date.getHours()-9;
                    }
                    if(brazilHrs<0){
                        brazilHrs=brazilHrs+24;
                    }else{
                        brazilHrs=brazilHrs;
                    }
                /*printing 24hr format*/     
                showTime.innerHTML=brazilHrs+":"+brazilMns+":"+date.getSeconds();
            break;
            case "Japan(+9 GMT)":
                 /*converting minutes of Japan*/ 
                japanMns=date.getMinutes()+30;
                japanMns=japanMns%60;
                    /*converting hours of Japan*/ 
                    if(japanMns>=0 && japanMns<30){
                        japanHrs=date.getHours()+4;
                    }else{
                        japanHrs=date.getHours()+3;
                    }
                    if(japanHrs<0){
                        japanHrs=japanHrs+24;
                    }
                    else{
                        japanHrs=japanHrs;
                    }
                /*printing 24hr format*/     
                showTime.innerHTML=japanHrs+":"+japanMns+":"+date.getSeconds();            
            break;
            case "Canada(-4 GMT)":
                /*converting minutes of Canada*/ 
                canadaMns=date.getMinutes()+30;
                canadaMns=canadaMns%60;
                    /*converting hours of Canada*/ 
                    if(canadaMns>=0 && canadaMns<30){
                        canadaHrs=date.getHours()-9;
                    }else{
                        canadaHrs=date.getHours()-10;
                    }
                    if(canadaHrs<0){
                        canadaHrs=canadaHrs+24;
                    }else{
                        canadaHrs=canadaHrs;
                    }
                /*printing 24hr format*/ 
                showTime.innerHTML=canadaHrs+":"+canadaMns+":"+date.getSeconds();
            break;
            case "Aus(+10 GMT)":
                /*converting minutes of Aus*/ 
                ausMns=date.getMinutes()+30;
                ausMns=ausMns%60;
                    /*converting hours of Canada*/ 
                    if(ausMns>=0 && ausMns<30){
                        ausHrs=date.getHours()+5;
                    }else{
                        ausHrs=date.getHours()+4;
                    }
                    if(ausHrs<0){
                        ausHrs=ausHrs+24;
                    }else{
                        ausHrs=ausHrs;
                    }
                 /*printing 24hr format*/     
                showTime.innerHTML=ausHrs+":"+ausMns+":"+date.getSeconds();
            break;
            case "Qatar(+3 GMT)":
                 /*converting minutes of Qatar*/ 
                qatarMns=date.getMinutes()+30;
                qatarMns=qatarMns%60;
                     /*converting hours of Qatar*/ 
                    if(qatarMns>=0 && qatarMns<30){
                        qatarHrs=date.getHours()-2;
                    }else{
                        qatarHrs=date.getHours()-3;
                    }
                    if(qatarHrs<0){
                        qatarHrs=qatarHrs+24;
                    }else{
                        qatarHrs=qatarHrs;
                    }
                 /*printing 24hr format*/     
                showTime.innerHTML=qatarHrs+":"+qatarMns+":"+date.getSeconds();
            break;
            default:
                /*clearing div*/
                showTime.innerHTML="";
            break;    
        }
}
/*dropdownlist countries time dynamic*/
setInterval(findTime,1000);
    </script>
</head>
<body onload="bodyload()">
    <fieldset>
        <legend style="margin:auto">
            <b>India(+5:30 GMT)</b>
        </legend> 
        <table  width="300px" cellspacing="2px" cellpadding="2px">
            <tr>
                <td align="center">
                    <i>12 hr format</i>
                </td>
                <td align="center" style="width:180px">
                    <span align="center" id="msg1"  class="digiClock">
                    </span>
                    <span id="dayFormatIndia" class="digiClock">
                    </span>
                </td>

            </tr>
            <tr>
                <td align="center">
                    <i>24 hr format</i>
                </td>
                <td align="center" style="width:180px">
                    <span id="msg2" class="digiClock">
                    </span>
                </td>
            </tr>

        </table>   
    </fieldset>
        <fieldset>
        <legend style="margin:auto">
            <b>USA-EastCost(-4:00 GMT)</b>
        </legend> 
        <table  width="300px">
            <tr>
                <td align="center">
                    <i>12 hr format</i>
                </td>
                <td align="center" style="width:180px">
                    <span align="center" id="msg3" class="digiClock" >
                    </span>&nbsp
                    <span id="dayFormatUs" class="digiClock">
                    </span>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <i>24 hr format</i>
                </td>
                <td align="center">
                    <span id="msg4" class="digiClock">
                    </span>
                </td>
            </tr>

        </table>
    </fieldset>
    <fieldset>
        <legend style="margin:auto">
            <b>other countries(24 hr format)</b>
        </legend> 
        <table width="300px">
            <tr>
                <td >
                    <select id="country" onclick="findTime()">
                    </select>
                </td>
                <td align="left">
                    <span id="showTime" class="digiClock"></span>
                    <span id="formatTime">

                    </span>
                </td>
            </tr>
        </table>
    </fieldset>
</body>
</html>

Not the answer you're looking for? Browse other questions tagged or ask your own question.