1

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

Community
  • 1
  • 1
Amit
  • 31
  • 3

1 Answers1

1

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 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. – Amit Jul 03 '13 at 04:17
  • The date either in its various parts (year, month, day, hour, minute second), or in its *string* representation, will be relative to the local time specified by the time zone. The date in its *integer* representation will always be from UTC, regardless of time zone. Notice above that the same UTC timestamp is at 10:30 in Los Angeles, but 12:30 in Chicago. Changing time zones doesn't change the UTC value - if you did, that would be referring to a different moment in time. It just changes your local perspective. – Matt Johnson-Pint Jul 03 '13 at 05:26