Web Analytics Made Easy -
StatCounter UTC date and time object calculation - CodingForum

Announcement

Collapse
No announcement yet.

UTC date and time object calculation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • UTC date and time object calculation

    Just learning - I need to display the local and UTC date and time as well as the times for countries in other time zones and need the calculation to convert the UTC time (+ or -). I know, for example, that Washington DC is UTC-0500.

    I have created both the local and UTC time and have it displaying on my page as follows:

    Code:
    <html>
    <head>
    <script language="javascript">
    var months=new Array();
    months[0]="January";
    months[1]="February";
    months[2]="March";
    months[3]="April";
    months[4]="May";
    months[5]="June";
    months[6]="July";
    months[7]="August";
    months[8]="September";
    months[9]="October";
    months[10]="November";
    months[11]="December";
    
    function ourdate(){
    var time=new Date();
    var myMonth=months[time.getMonth()];
    var myDate=time.getDate();
    var myYear=time.getFullYear();
    var hr=time.getHours();
    var min=time.getMinutes();
    var sec=time.getSeconds();
    var temp = "" + ((hr > 12) ? hr - 12 : hr)
      if(hr==0) temp = "12"
      if(temp.length==1) temp = " " + temp
      temp += ((min < 10) ? ":0" : ":") + min
      temp += ((sec < 10) ? ":0" : ":") + sec
      temp += (hr >= 12) ? " PM" : " AM"
    document.getElementById('current').innerHTML = myYear + " " + myMonth + " " + myDate + "<br />" + temp
    	setTimeout("ourdate()", 1000)
    
    }
    
    function UTCdate(){
    var UTCtime=new Date();
    var myUTCMonth=months[UTCtime.getMonth()];
    var myUTCDate=UTCtime.getUTCDate();
    var myUTCYear=UTCtime.getUTCFullYear();
    var UTChr=UTCtime.getUTCHours();
    var UTCmin=UTCtime.getUTCMinutes();
    var UTCsec=UTCtime.getUTCSeconds();
    var UTCtemp = "" + ((UTChr > 12) ? UTChr - 12 : UTChr)
      if(UTChr==0) UTCtemp = "12"
      if(UTCtemp.length==1) UTCtemp = " " + UTCtemp
      UTCtemp += ((UTCmin < 10) ? ":0" : ":") + UTCmin
      UTCtemp += ((UTCsec < 10) ? ":0" : ":") + UTCsec
      UTCtemp += (UTChr >= 12) ? " PM" : " AM"
    document.getElementById('UTC').innerHTML = myUTCYear + " " + myUTCMonth + " " + myUTCDate + "<br />" + UTCtemp
    	setTimeout("UTCdate()", 1000)
    }
    
    </script>
    </head>
    <body onLoad="ourdate(); UTCdate()">
    <Form name="clock">
    <SELECT name=selectplace onChange="setPlace()"> 
    <OPTION value=0 selected>Paris, France</OPTION>
    <OPTION value=1>Washington,DC U.S.A.</OPTION>
    <OPTION value=2>Mexico City, Mexico</OPTION>
    <OPTION value=3>Nairobi, Kenya</OPTION>
    <OPTION value=4>Hanoi, Vietnam</option>
    <option value=5>Helsinki, Finland</option>
    <option value=6>Hong Kong, China</option>
    <option value=7>Wellington, New Zealand</option></SELECT>
    <p>
    <p>
    <table>
    <Table border=2>
    <td></td>
    <td>Your Time</td>
    <td>City drop down menu</td>
    <tr>
    <td height="40">Local
    </td>
    <td id="current"></td>
    <td id="droplocal"></td>
    <tr>
    <td>UTC</td>
    <td id="UTC"></td>
    <td id="dropUTC"></td>
    </table>
    </body>
    </html>
    Thank you.
Working...
X