2

I have a timestamp, which I'm formatting with momentjs:

moment("2015-03-24T09:47:31.042Z").format("LLL Z")
"March 24, 2015 10:47 AM +01:00"

Due to the nature of our application, we're setting a default timezone for momentjs, which affects all rendered moments:

moment.tz.setDefault("America/Los_Angeles")
moment("2015-03-24T09:47:31.042Z").format("LLL Z")
"March 24, 2015 2:47 AM -07:00"

So far, so great. But now I want to display the local time in one specific spot in the application, but momentjs will of course use the provided default timezone:

moment().format("LLL Z")
"March 24, 2015 2:52 AM -07:00"

How can I construct a moment instance that uses the local timezone again?

I tried using moment.tz(timestamp,null), but that simply uses GMT.

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138

1 Answers1

1

Thanks to the suggestion by @Tanner in the comments and the answers in Getting the client's timezone in JavaScript, I was able to figure it out.

You can set the UTC offset of a moment using utcOffset. Combined with Date.getTimezoneOffset, you can render the moment in local time again:

moment("2015-03-24T09:47:31.042Z")
  .utcOffset(-new Date().getTimezoneOffset())
  .format("LLL Z")

"March 24, 2015 10:47 AM +01:00"
Community
  • 1
  • 1
Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138