Reputation: 31
I am trying to use timezone-js to convert the timezone and get the correct time based on region. I am using following thread as reference. convert-date-to-another-timezone-in-javascript
But, when I set a time in constructor and change the timezone, it shows the same time.
I am not able to get the converted time based on the zone. I tried following.
var dt = new timezoneJS.Date("2013/07/02 18:59:00 +0000", 'Asia/Singapore');
alert('This is time call ' + dt.getTime() + dt.getTimezone());
dt.setTimezone("America/Argentina/Mendoza");
alert('This is new time call ' + dt.getTime() + dt.getTimezone());
Both shows me time as 1372791540000. Am I missing anything here?
Thanks Matt for the answer. That did give me the date. But, I am surely missing the concept to use this library. My understanding was that once I give a time and zone to the constructor, it will automatically adjust the UTC in the object, and next time I change the zone, it will provide me the correct time as per the zone.
e.g. - var dt = new timezoneJS.Date("2002/07/08 18:59:00 +0000", 'Asia/Singapore');
This gives me 2002-07-09 02:59:00
So, it is taking first argument(date) as the UTC and adjusting time accordingly. Is this correct behavior?
Then dt.setTimezone("America/Argentina/Mendoza")
;=>2002-07-08 15:59:00
Upvotes: 1
Views: 1202
Reputation: 241420
getTime()
will always return milliseconds since Jan 1, 1970 UTC. There are no time zones when expressing time as a single integer.
In the documentation, you will find the following example as proof:
var dtA = new timezoneJS.Date(2007, 9, 31, 10, 30, 'America/Los_Angeles');
var dtB = new timezoneJS.Date(2007, 9, 31, 12, 30, 'America/Chicago');
// Same timestamp
dtA.getTime(); => 1193855400000
dtB.getTime(); => 1193855400000
For time zone specific output, try .toString()
instead.
Upvotes: 1