moment-timezone: Wrong behavior when creating a moment with timezone

According to the documents:

moment.tz("2013-11-18 11:55", "America/Toronto").format(); // "2013-11-18T11:55:00-05:00"

But running this code in my browser with locale UTC +0100 gives me this

moment.tz("2013-11-18 11:55", "America/Toronto").format();
"2013-11-18T05:55:00-05:00"

seems to me that it first creates a new Moment with my systems locale and then change the timezone which does not provide the expected behavior.

If however I use my own timezone it works as expected and I think that’s what the author of the docs did.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

@andymcfee - There’s no bug, and you don’t need the hack. You simply need to provide a format string. Moment only recognizes ISO8601 unless you tell it otherwise. You’ll notice your code throws a deprecation warning onto the console telling you it’s reverting to the date constructor.

You should do:

moment.tz('30/09/2015 10:00 am', 'DD/MM/YYYY h:mm a', 'Europe/Dublin')

Also note that the “output” at this state is a moment object, not a string. You need to then call format, and provide the output format you’d like.