moment.js timezones .valueOf() not returning expected values

Corey picture Corey · Apr 21, 2015 · Viewed 13.1k times · Source

I have an angular application using highcharts-ng to make a line graph. The y-axis is numbers and the x-axis is datetime's.

I am trying to properly account for Daylight Savings Time changes when converting between the two timezones of "America/New_York" and "Europe/London" using moment.js.

London is currently in BST (as of the time of posting this), so it is +1:00.

tick.TimeStamp
> "2015-04-21T16:06:06.0786392-04:00"

tick.TimeStamp is my "America/New_York" time (currently in EDT). I convert that to London time using...

moment(tick.TimeStamp).tz("Europe/London").format()
> "2015-04-21T21:06:06+01:00"

I need my result in Unix Epoch ticks to plot them for the x-axis in highcharts-ng, so I use...

var d = moment(tick.TimeStamp).tz("Europe/London").format()
moment(d).valueOf()

which yields

1429646766000

The issue is that this tick value result as a datetime is

Tue, 21 Apr 2015 20:06:06 GMT

where it should be

Tue, 21 Apr 2015 21:06:06 GMT

since London is currently in BST +1:00

Am I doing something wrong, or is moment just calculating this incorrectly?

Any help would be greatly appreciated. Thank you!

EDIT: I should mention that my moment-timezones.js is the most recent from their site with all of the timezone info.

Answer

rob picture rob · Apr 21, 2015

Moment is calculating this correctly.

Tue, 21 Apr 2015 20:06:06 GMT, Tue, 21 Apr 2015 21:06:06 BST, and Tue, 21 Apr 2015 16:06:06 EDT all refer to the same time and will all have the same unix timestamp. When you call .tz() you are just changing how that time will be formatted. You aren't changing the actual time.

Note: To get the unix time stamp you can use .unix() e.g.

moment(tick.TimeStamp).unix()

Or this will return the same value

moment(tick.TimeStamp).tz("Europe/London").unix()