Skip to content Skip to sidebar Skip to footer

Creating A Date Object In A Specific Time Zone

I need to pass in a Javascript Date object into a Angular model so that it can display the date and time to the user in a date/time picker. The data is stored in the database in th

Solution 1:

How can I create a Javascript Date object that properly represents the String passed in and respects the timezone information?

What do you mean by "respects the timezone information"? If you mean "continues to use an offset of -05:00", then that is fairly straight forward: parse the string, store the offset and use it whenever creating a timestamp from the date.

However, if you also want to observe daylight saving changes, you have a much bigger challenge. Assuming "CDT" is "Central Daylight (Saving) Time", then some places that use it are north of the equator (e.g. places in USA) and some places are south of the equator, e.g.

PlaceCDTstartsCDTendsTabasco,Mexico:2Apr2017    29Oct2017Chicago,USA:12Mar2017     5Nov2017EasterIs,Chile:12Aug2017    12May2018<--Southernhemisphere

So as you can see, just because a place uses CDT doesn't mean you can determine when it changes from CDT to CST.

The above is the reason why it is now common to use the IANA timezone database location identifiers (see Wikipedia). It allows a user to specify particular offsets and rules based on a location rather than a specific timezone.

So unless you can resolve "CDT -05:00" to, say, "America/Chicago" or "Chile/EasterIsland", you can either keep "CDT -05:00" for all timestamps, use UTC, or use UTC and convert to "local" based on the host system.

Post a Comment for "Creating A Date Object In A Specific Time Zone"