csvan
csvan

Reputation: 9454

Moment Timezone: varying (and wrong) results

I am using moment.js coupled with Moment Timezone in order to process ISO dates in different timezones. I must be doing something wrong, however, because the results I get from parsing are completely wrong, even if I to the best of my knowledge am not doing anything strange.

For example, take this date: 2015-11-23T08:56:38.646Z

Now, a normal parse and format outputs what can be expected:

moment("2015-11-23T08:56:38.646Z").format()

Result: "2015-11-23T09:56:38+01:00"

However, if I bring a non-local TZ into the mix, things go wrong really fast:

moment("2015-11-23T08:56:38.646Z","America/New_York").format()

Result: "2014-01-01T00:20:00+01:00"

Likewise:

moment("2015-11-23T08:56:38.646Z","Europe/Zagreb").format()

Result: "2015-01-17T01:00:00+01:00"

I would very much appreciate if someone could help me understand what is going on here.

Upvotes: 2

Views: 99

Answers (1)

csvan
csvan

Reputation: 9454

The solution is trivial and was due to a careless mistake on my part. I am leaving it here for the benefit of any scarce person who may do the same thing.

The correct way to use timezone conversion is to call moment.tz() rather than just moment. Hence, for example:

moment("2015-11-23T08:56:38.646Z","America/New_York").format()

should be

moment.tz("2015-11-23T08:56:38.646Z","America/New_York").format()

Upvotes: 2

Related Questions