Kylie
Kylie

Reputation: 11749

Moment timezone is confusing, no idea how to make it work

I have a UTC timestamp coming from the database

My local time here is America/Vancouver.

So for example, in the database, I have

   2018-12-26 08:28:00

I want this to display as

2018-12-25 12:28:00

8 hours ahead.

Currently I have done this

return moment(timestamp).tz('America/Vancouver').format()

And this returns

2018-12-26 08:28:00 -08:00

Which is correct, its showing the - eight hours. But how do I get it to actually display this as the correct time, including that 8 hour offset?

I thought moment was supposed to do that for you automatically? But it doesnt seem to.

 moment(timestamp).tz('America/Vancouver').fromNow()

gives me "In 8 hours". Whereas it should give me "11 minutes ago"

How do I get moment-timezone to actually do its thing?? According to examples what Im doing should be working, but its not.

For exmaple.

return moment(timestamp).tz('America/Vancouver').format('MM-DD-YYYY H:i:s)

returns

12-26-2018 08:26:00 

Which is the UTC value, not the offseted Vancouver value??? What gfives?

Upvotes: 2

Views: 235

Answers (1)

Vadim Hulevich
Vadim Hulevich

Reputation: 1833

Moment.js will parse dates with timezone which it can detect on your computer. if you need parse UTC date you need use .utc method.

moment.utc(timestamp).tz('America/Vancouver').format()

Upvotes: 5

Related Questions