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!

worldtime clock [AS2/AS3]

Status
Not open for further replies.

santanudas

Technical User
Mar 18, 2002
121
GB
Hi people,

I'm very very new in this flash world (just a 2 weeks old of experiences, so far) and I found the ActionScript (especially v3) is really cool. So I started with making clock, not the the one which takes time off the system clock but a clock with different timezone(s). I didn't find any clean straight way of doing that other than adding or subtracting the time difference(s) to/from UTC time. Is that only way of doing this in flash?

While doing that, I found it's very easy for the time zones those have compete hours of difference; i.e. Tokyo: +9 or NYC: -4 and I do like this:
Code:
var now:Date = new Date();
var utc_date = now.getUTCDate();
var utc_sec = now.getUTCSeconds();
var utc_min = now.getUTCMinutes();
var utc_hrs = now.getUTCHours();

var new_seconds = utc_sec;
var nw_minutes = utc_min;
var new_hours = utc_hrs+9;
and it works absolutely fine. But the problem I face for the time zones have hours and half difference; i.e. India: -5:30 [five & half hrs of difference]. How can I do that? Any help/hint(s) would be very much appreciated.

Sorry, if my question sounds silly and many thanks in advance for replying. Cheers!!!


 
Code:
var now:Date = new Date();
trace(now.toUTCString());

var newNow:Date = new Date(now.valueOf() + 330*60*1000);
trace(newNow.toUTCString());

Output
Tue Sep 18 14:00:17 2007 UTC
Tue Sep 18 19:30:17 2007 UTC

Kenneth Kawamoto
 
first of all, thank you very much guys for your replies.

oldnewbie,
Adding 30 to the UTC minutes not gonna work; I tried that before. You will see some weird result if you make a digital clock.

kennethkawamoto,
toUTCString() way is the possible workaround but it’s only supported in AS3. Due to lack of AS3 documentations/tutorials, I already started the project in AS2 and it’s actually a lot of work moving to AS3. Do you know any possible workaround for AS2?
 
thanks kennethkawamoto. I actually did this:
Code:
var now:Date = new Date();
var utcDate = now.toUTCString();

var newNow:Date = new Date(utcDate.valueOf() + 330*60*1000);
trace (newNow);
returns "Invalid Date" in the end. Can't I do the addition stuff on UTC?
 
kennethkawamoto, adding time offset to the local time is not a very good idea, I believe. The problem will kick-off during the DST change. So, don't you think using UTC would be good?
Cheers!!!

 
No. I think you're confused.

Back to AS3 (due to easy [tt]toUTCString()[/tt] method):
Code:
var now:Date = new Date();
trace(now);
trace(now.toUTCString());

var newNow:Date = new Date(now.valueOf()+330*60*1000);
trace(newNow);
trace(newNow.toUTCString());
Output:
Tue Sep 18 18:07:50 GMT+0100 2007
Tue Sep 18 17:07:50 2007 UTC
Tue Sep 18 23:37:50 GMT+0100 2007
Tue Sep 18 22:37:50 2007 UTC

The new Date is 5 hours 30 minutes later both in local time and UTC (GMT) time.

Kenneth Kawamoto
 
Okay, let me explain..........
I did exactly the same thing as you did, to test. Now as far as I understand, Date() returns the local time, based on the computer system time. Take that 5 & half hour offset example; in London, now it's BST, which is GMT+1 hr. and with India (IST) the time difference is 4 hrs. and 30 minutes. In October, BST switches back to GMT but the IST doesn't change. There are a number of the countries in the world where DST is not followed. So, in summer, w.r.t local time (i.e. BST) the offset is 4 and half and in the winter its 5 and half. So, if you add 5 and half to BST, then you will be showing wrong time in the summer. But if you think about GMT it always 5 and half hrs. difference.

But the probably, the biggest problem is, if you make your script based on BST as the local time and then run the program in the NYC (suppose you making the world clock for your website), then you are not gonna see the correct time for other time zones. But if the calculations is based on GMT/UTC, you don't have to worry about that.

Am I right??
 
The offset from local time varies depending on the DST, but that's the same for UTC. The offset from UTC varies depending on the DST.

But for India, since her DST has been abolished, you don't have to worry about it. The offset from UTC is always +5 hours 30 minutes.
Code:
var now:Date = new Date();
trace("Local: "+now.getFullYear()+" "+now.getMonth()+" "+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+":"+now.getMilliseconds());

var indiaNow:Date = new Date(now.valueOf()+now.getTimezoneOffset()*60*1000+330*60*1000);
trace("India: "+indiaNow.getFullYear()+" "+indiaNow.getMonth()+" "+indiaNow.getDate()+" "+indiaNow.getHours()+":"+indiaNow.getMinutes()+":"+indiaNow.getSeconds()+":"+indiaNow.getMilliseconds());
Output:
Local: 2007 8 19 0:0:9:428
India: 2007 8 19 4:30:9:428

India is 4 hours 30 minutes ahead from my local time, which is BST currently observing DST (+ 1 hour).

Now I set my computer date to November, when there's no DST in BST.

Output:
Local: 2007 10 19 0:9:8:145
India: 2007 10 19 5:39:8:145

India is 5 hours 30 minutes ahead of BST in November.


Kenneth Kawamoto
 
Many thanks Kenneth,

getTimezoneOffset() did the trick well. My local time is BST too, I got it working here and I checked with two of my friends in NYC and Seattle and they got the right time too. But one thing I'm confused about: When we say Date() takes the system time, do we refer the client machine (i.e. viewers' computer) or the server (i.e. the machine hosting the flash movie) as the "system"? The value, returned by Date() isn't different in NYC than mine? Documentation says, getTimezoneOffset() returns the difference between universal time (UTC) and the computer's local time. Does it mean my computer or my friend's?

What am I missing? Thanks a lot for helping.

 
Date object is based on your local computer settings, not from the server or anything. [tt]getTimezoneOffset()[/tt] is based on the settings of your OS. To verify, change your clock and create a Date object.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top