Reputation: 4425
I'm working in a scheduler web application and my client (Angular) and server (Asp.net core) timezones are different. The client is in any timezone. Let´s use (GMT-3). The server is UTC.
Let´s suppose this case:
One user schedule an event to it´s local time at 08:00AM. When send this information to serve, it will save 11:00AM in database.
So, when user retrieve this information, client will convert back to 08:00AM due to -3 hours timezone.
But, if this schedule was made to a date in future, when client's country will be in daylight saving, it will convert back to -2 hours. So it will converted to 09:00AM to the client, and that is wrong.
How to deal with daylight saving time when I get dates from server?
Upvotes: 0
Views: 2074
Reputation: 2137
If I understand the issue correctly, you want to keep the server using UTC stored date/times and have the client display local time while handling the DST. I recommend using the angular2-moment
, Moment
& Momemt-Timezone
npm packages. This package will be able to automatically handle the DST when you provide the iana_timezone like America\Chicago
.
moment.tz(<utc-date-time>, <iana-timezone>).format()
This will handle all the necessary conversions you need in the client.
Also checkout the Moment Timezone Docs
Upvotes: 2
Reputation: 239250
Simply, date and times should be stored in UTC. You can always get from UTC back to the user's time. The problem with storing a datetime with an offset is that the offset is not contextual. For example, let's assume that the user is in DST with a timezone that is normally -3 offset from UTC. As such, their current offset is -2. You store the -2 offset, and now what? Is it -2 because they're in a zone that's -2 or is it -2 because it's a -3 zone in DST. There's no way to know. In effect, you've failed to capture critical information.
You don't have this issue with datetimes stored in UTC. You can simply get the user's current time, including their current offset (DST or not) and compare that with the times in your data store. You may need to convert the user time to UTC first, but in many case you do not. For example, the DateTimeOffset
type is smart enough to be able to compare taking offset into account. Many databases support this as well for offset-capable column types.
Upvotes: 3