Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to keep backend timezone? #4799

Closed
Shoroh opened this issue Oct 4, 2018 · 4 comments
Closed

Is it possible to keep backend timezone? #4799

Shoroh opened this issue Oct 4, 2018 · 4 comments

Comments

@Shoroh
Copy link

Shoroh commented Oct 4, 2018

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!

@simonhaenisch
Copy link

simonhaenisch commented Oct 7, 2018

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");

@Shoroh
Copy link
Author

Shoroh commented Oct 7, 2018

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.

@ashsearle
Copy link
Contributor

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"

@Shoroh Shoroh closed this as completed Oct 8, 2018
@hinst
Copy link

hinst commented Jun 29, 2023

I know this is an old issue but let me just mention for posterity that it is mind blowing how framework can make weird decisions such as this one

Why change timezone? The last thing you expect when parsing a date with time zone, is for it to get magically changed. When you parse "2018-10-10T13:00:00+11:00" and it says +11:00 then the outcome should be timzeone=+11:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants