Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two-Time-Zones Concurrent Display

Status
Not open for further replies.

kennybak

Technical User
Nov 19, 2001
3
0
0
US
I would like to display the date/time (ex. Nov 22, 2001 2:30 AM EST) of two time zones (GMT-10, HST) and GMT-5, at present EST), concurrently on my 'weather' page, from one script. I have posted on several forums and some say I need to use server side script and others say that the user's locale/OS poses a problem in writing a script. I am just learning Javascript and am asking for a script, rather than submitting one for critique. Jaroslav Stika ( has written a World Time Clock script that is very, very complex -- much more complex than I need. Also, in his program, my time zone is displayed correctly, but it is then necessary for me to input my city from a drop-down list (time zone reference) in order for the program to proceed.

Would appreciate any comments/suggestions regarding the capability of Javascript for a two-time-zone variation of the basic (though individually customized) date/time clock script.

Thank you,
Ken Baker
 
Here's a simple script using timezoneoffset as a variable :


<script language=&quot;JavaScript&quot;>
function datetime (foo) {
var dateToday = new Date();
dateToday.setTime(dateToday.getTime()+foo*60*60*1000);
var amOrPm;
hoursFinal = dateToday.getHours();
if (hoursFinal < 12) {
amOrPm = &quot;am&quot;;
}
else {
hoursFinal = hoursFinal - 12;
amOrPm = &quot;pm&quot;;
}
if (hoursFinal == 0) {
hoursFinal = 12;
}
minutesFinal = dateToday.getMinutes();
minutesFinal = minutesFinal.toString();
if (minutesFinal.length == 1) {
minutesFinal = '0'+minutesFinal;
}
secondsFinal = dateToday.getSeconds();
secondsFinal = secondsFinal.toString();
if (secondsFinal.length == 1) {
secondsFinal = '0'+secondsFinal;
}
current_time = hoursFinal+':'+minutesFinal+'.'+secondsFinal+amOrPm+' ';
var dayFinal = dateToday.getDay();
var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
dayFinal = dayNames[dayFinal];
var dayNumber = dateToday.getDate();
var monthFinal = dateToday.getMonth();
var monthNames = ['January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
monthFinal = monthNames[monthFinal];
var yearFinal = dateToday.getFullYear();
return current_time+dayFinal+' '+dayNumber+' '+monthFinal+', '+yearFinal;
}
</script>

Time -12 hours : <script language=&quot;JavaScript&quot;>document.write(datetime(-12));</script><br>
Time -8 hours : <script language=&quot;JavaScript&quot;>document.write(datetime(-8));</script><br>
Time -4 hours : <script language=&quot;JavaScript&quot;>document.write(datetime(-4));</script><br>
Time 0 hours : <script language=&quot;JavaScript&quot;>document.write(datetime(0));</script><br>
Time +4 hours : <script language=&quot;JavaScript&quot;>document.write(datetime(4));</script><br>
Time +8 hours : <script language=&quot;JavaScript&quot;>document.write(datetime(8));</script><br>
Time +12 hours : <script language=&quot;JavaScript&quot;>document.write(datetime(12));</script> Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Hi David, thanks for replying to my post and taking the time to write a script for me that appears to be just what I'm looking for.

Simple script? Ha! Easy for you say. I'm trying to learn it so it's not simple to me. I will copy it to a test page and give it a shot. I will probably have some trouble implementing it, so what should I do? Other posts are popping up as I type this to which you will direct your expertise and my post is scrolling away fast getting less attention rapidly. Can I email you should I need help or should I continue the thread for that purpose?

Thanks again for the script
Ken Baker
 
David, I've tweaked your script to obtain the following format --


-- however the time zones I select for the five hour offset don't correspond to the real GMT offsets for the two zones: Maryland (-5) and Hawaii (-10). Instead, I had to use Maryland (0) and Hawaii (-5). Is this how your program is supposed to work? I'm confused on this point. How would browers at, say, a) Honolulu (GMT-10) and b)'Boondocks,' USA (GMT-8) display my page?

I would like a live clock, but a live clock function cannot be embedded into HTML, right? Can I use a form near a table or in a table to achieve that function? Can I put a form box into a cell or row? (I'm just learning so they're probably dumb questions :-(.)

Your script is a beautiful piece of work!! I can't thank you enough for sharing your tremendous Javascript knowledge with me for my little project.

Ken Baker
 
The date() function depends on the users clock, so timezone offsets are not accurate, the only way to get accurate timezoneoffset, is to calculate the timezone from a server at 0 hrs GMT, ie here in the UK (I live very close to Greenwich)

You can use a 'Real Time' clock using div's you can update 'inner html' in the div, the other way is to update in a text input field (you could style it with CSS)

If you need any more help just e-mail me. Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top