Part of my app stores the dates and times of events. I store them in UTC time in the database, and use browser time zone information to translate that into their local time.
All works well… except for when daylight savings gets involved.
The issue is that when my app displays the date and time of an event in the future, it may be on the other side of the time change… so my app needs to know if a future time will be in a different UTC offset due to daylight savings.
How do you handle this situation?
I have an example below!
Best wishes,
Antony.
==========
An example, based on UK times.
In December, I define an event which runs at 09:00 on 30th April the next year.
My current time zone is GMT, which is the same as UTC. So the time of the event is stored as 09:00.
However at the end of March, the time zone changes to BST, which is 1 hour ahead of UTC. Suddenly the event is published as being held at 10:00.
So my app needs a way to know that the time I am holding the event will be in a different timezone… and therefore to always:
Save in UTC relative to the time zone when the event runs rather than the timezone now.
Display it in the user’s local timezone on the date of the event, rather than their current local time zone.
This will display the correct time allowing for BST (if needed). If you display using offset it will be fixed to the offset value:
moment().utcOffset("2013-03-07T07:00:00+08:00")
From what you’ve said the timezone is the users (rather than linked to the event, which would need to be saved/retrieved from the DB). It sounds like you have a method to get the timezone in place already, in which case it’s just using the moment timezone formatter rather than offset.
If you have an UTC string and format it on the client with App Connect it will be converted to the local timezone of the user. You don’t have to worry about DST.
If you want to display it in a different timezone that the client current timezone, you can use Intl.DateTimeFormat. You can create a custom formatter that formats the date in the correct language and correct timezone. Intl is supported on all mayor browsers, as long you don’t depend on old IE users you’re fine.