[Javascript] Javascript World Times

shawn_milochik at godivachoc.com shawn_milochik at godivachoc.com
Fri Apr 30 11:07:02 CDT 2004





Here is a function to display world times.  I'm proud of some of the work
there, but I suspect that some of
you may have improvements to suggest.  The rules for time zones were taken
from:
http://timeanddate.com/worldclock/

Just go there, click the city/country in which you are interested, and the
offset rules and dates will be there.

Feedback extremely welcome!

Shawn









///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Here is the HTML part:

In body tag:
onload="worldTimes();"

In body:
(I am using style sheets to format and align the block of times to the
center.
All the <div> elements can be removed at your preference, but the <span>
elements are required.

          <!-- ##### World Times Begin ##### -->
         <div style="text-align: center; width: 99%;">

            <div style="width: 500px;">
               <div class="floatTime">
                  New York<br/> <span id="rh1"></span>
               </div>
               <div class="floatTime">
                  Brussels<br/> <span id="bh1"></span>
               </div>
               <div class="floatTime">
                  Hong Kong<br/> <span id="hh1"></span>
               </div>
               <div class="floatTime">
                  Tokyo<br/> <span id="th1"></span>
               </div>
               <div class="floatTime">
                  Sydney<br/> <span id="sh1"></span>
               </div>
            </div>
         </div>
         <!-- ##### World Times End ##### -->



Javascript function (worldTimes.js)

function textDay(weekDay){

   var textDay;

   if (weekDay == 0){ textDay = "Sun"; }
   if (weekDay == 1){ textDay = "Mon"; }
   if (weekDay == 2){ textDay = "Tue"; }
   if (weekDay == 3){ textDay = "Wed"; }
   if (weekDay == 4){ textDay = "Thu"; }
   if (weekDay == 5){ textDay = "Fri"; }
   if (weekDay == 6){ textDay = "Sat"; }

   return textDay;

}

function worldTimes(){

   var local = new Date();
   var UCT;
   var localOffset;
   var startDST;
   var endDST;

   var Brussels;
   var Sydney;
   var Reading;
   var Tokyo;
   var HongKong;

   var BrusselsOffset = + 1;
   var SydneyOffset = + 10;
   var HongKongOffset = + 8;
   var ReadingOffset = - 5;
   var TokyoOffset = + 9;

   //This sets localOffset to the number of minutes difference
   //between local time and UCT
   localOffset = local.getTimezoneOffset();
   //Convert localOffset to milliseconds
   localOffset = localOffset * 60 * 1000;

   //Make 'UCT' a number, instead of a date variable.
   UCT = local - 1;

   //adjust for localOffset
   UCT = UCT + localOffset;

   //Convert UCT back into a date variable
   UCT = new Date(UCT);


   //figure out Daylight Saving Time info


   //Reading DST
   startDST = new Date("4/1/" + UCT.getFullYear());
   endDST = new Date("11/1/" + UCT.getFullYear());

   while(startDST.getDay() != 0){
      startDST = startDST - 1;
      startDST = new Date(startDST + 60 * 60 * 1000);
   }

   while(endDST.getDay() != 0){
      endDST = endDST - 1;
      endDST = new Date(endDST - 60 * 60 * 1000);
   }

   if ((UCT >= startDST) && (UCT < endDST)){
      ReadingOffset = ReadingOffset + 1;
   }



   //Brussels DST
   startDST = new Date("4/1/" + UCT.getFullYear());
   endDST = new Date("11/1/" + UCT.getFullYear());

   while(startDST.getDay() != 0){
      startDST = startDST - 1;
      startDST = new Date(startDST - 60 * 60 * 1000);
   }

   while(endDST.getDay() != 0){
      endDST = endDST - 1;
      endDST = new Date(endDST - 60 * 60 * 1000);
   }

   if ((UCT >= startDST) && (UCT < endDST)){
      BrusselsOffset = BrusselsOffset + 1;
   }



   //Sydney DST

   startDST = new Date("11/1/" + UCT.getFullYear());
   endDST = new Date("4/1/" + UCT.getFullYear());

   while(startDST.getDay() != 0){
      startDST = startDST - 1;
      startDST = new Date(startDST - 60 * 60 * 1000);
   }

   while(endDST.getDay() != 0){
      endDST = endDST - 1;
      endDST = new Date(endDST - 60 * 60 * 1000);
   }

   if ((UCT >= startDST) || (UCT < endDST)){
      SydneyOffset = SydneyOffset + 1;
   }



   //Get world times
   Reading = UCT - 1;
   Reading = new Date(Reading + (ReadingOffset * 60 * 60 * 1000));
   Brussels = UCT - 1;
   Brussels = new Date(Brussels + (BrusselsOffset * 60 * 60 * 1000));
   Sydney = UCT - 1;
   Sydney = new Date(Sydney + (SydneyOffset * 60 * 60 * 1000));
   HongKong = UCT - 1;
   HongKong = new Date(HongKong + (HongKongOffset * 60 * 60 * 1000));
   Tokyo = UCT - 1;
   Tokyo = new Date(Tokyo + (TokyoOffset * 60 * 60 * 1000));

   showTime(Reading, 'r');
   showTime(Brussels, 'b');
   showTime(Tokyo, 't');
   showTime(Sydney, 's');
   showTime(HongKong, 'h');

   setTimeout('worldTimes()', 1000);
}

function showTime(dateTime, prefix){

   //alert('c');

   var hr1 = document.getElementById(prefix + 'h1');
   var ampm;

   var x;
   var y;
   var hours;
   var minutes;
   var seconds;
   var weekDay;

   hours = dateTime.getHours();
   minutes = dateTime.getMinutes();
   seconds = dateTime.getSeconds();
   weekDay = dateTime.getDay();


   //Hours

   ampm = 'AM';



   if (hours == 0){
      hours = 12;
   }

   if (hours > 12){
      ampm = 'PM';
      hours -= 12;
   }





   //Minutes
   if (minutes < 10){
      minutes = '0' + minutes;
   }




   //Seconds
   if (seconds < 10){
      seconds = '0' + seconds;
   }



   //hr1.innerHTML = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
   hr1.innerHTML = hours + ':' + minutes + ' ' + ampm + ' ' +
textDay(weekDay);




}







**********************************************************************
This e-mail and any files transmitted with it may contain 
confidential information and is intended solely for use by 
the individual to whom it is addressed.  If you received
this e-mail in error, please notify the sender, do not 
disclose its contents to others and delete it from your 
system.

**********************************************************************




More information about the Javascript mailing list