Moment: Is it possible to keep backend timezone?

Created on 4 Oct 2018  ·  3Comments  ·  Source: moment/moment

Description of the Issue and Steps to Reproduce:
Say I received a date from backend in this format:

"2018-10-10T13:00:00+11:00"

What I expect to see in frontend after parsing and formatting:

10-10-2018 1.00 PM

What I got:

10-10-2018 5.00 AM

I don't care what my system time zone is. I don't want to see UTC time either. How can I achieve it?

Please include the values of all variables used.

See above.

Environment:

It doesn't matter, it shouldn't depend on the environment. That is the issue, btw, cause it depends!

Most helpful comment

You could try parseZone:

e.g. for a locale timezone in BST (UTC + 1)

moment.parseZone("2018-10-10T13:00:00+11:00").format(); // "2018-10-10T13:00:00+11:00"
moment.parseZone("2018-10-10T13:00:00+11:00").local().format(); // "2018-10-10T03:00:00+01:00"
moment.parseZone("2018-10-10T13:00:00+11:00").utc().format(); // "2018-10-10T02:00:00Z"

All 3 comments

Afaik, moment defaults to using the device's timezone when formatting a date (which is also the standard behaviour of Javascript). So yes, formatting a date should depend on the environment. For example, on my device I get this for your date, which is the correct time for my local time (Auckland):

moment("2018-10-10T13:00:00+11:00").format()
// => '2018-10-10T15:00:00+13:00'

If you want to set a default timezone for the user manually, you can use moment-timezone with an IANA timezone name:

moment.tz.setDefault('America/New_York');

That will enforce all formatting to be done for that timezone and be independent of the environment.

Here's a link to the docs: https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/


It's not possible to find out the user's timezone from the offset (because multiple timezones can share the same offset, e. g. depending on daylight saving time) but you can use the Etc zone names for "generic" zones, e. g. Etc/GMT-11 in your case (the sign is inverted for those zones). In your case it would probably make more sense to store the user's timezone as a setting and persist it on the server, or save it along with the timestamp.

Then you can print a single date in a certain timezone by using e. g.

moment.tz("2018-10-10T13:00:00+11:00", "America/New_York");

Thanks, @simonhaenisch

I've seen tz plugin, but I thought it helps to manage time zones, like converting from one to another . And it sounds weird to me to use it just to NOT touch time zone at all :)

If you want to set a default timezone for the user manually

Unfortunately, there is no a default time zone in the server. Each user has their own timezone, saved in profile. And frontend knows nothing about it.

It's not possible to find out the user's timezone from the offset

That's ok, I don't care about the timezone. All I need is just to keep the coming date without changes.

store the user's timezone as a setting and persist it on the server, or save it along with the timestamp.

This is what exactly happening. The timezone is a setting and the date saved with it as a timestamp.

Since the backend API is under my control I'm able just to send reduced version of date, without time zone offset. I hope it helps.

But, it's really interesting why the moment.js doesn't have something like that:

moment(somDateTime).format({ formatWithDeviceTimezone: true })

or kind of :)

Where formatWithDeviceTimezone is false by default.

Anyway, thanks, now I see the issue.

You could try parseZone:

e.g. for a locale timezone in BST (UTC + 1)

moment.parseZone("2018-10-10T13:00:00+11:00").format(); // "2018-10-10T13:00:00+11:00"
moment.parseZone("2018-10-10T13:00:00+11:00").local().format(); // "2018-10-10T03:00:00+01:00"
moment.parseZone("2018-10-10T13:00:00+11:00").utc().format(); // "2018-10-10T02:00:00Z"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tanepiper picture tanepiper  ·  3Comments

RobinvanderVliet picture RobinvanderVliet  ·  3Comments

BCup picture BCup  ·  3Comments

danieljsinclair picture danieljsinclair  ·  3Comments

dbshwang picture dbshwang  ·  3Comments