0

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 the following format:

date: Sat, 23 Sep 2017 21:00:00 CDT -05:00,

This data is passed into an Angular directive, where I need to create a Date object representing it. The issue is that whenever a new Date() is instantiated, it always uses the timezone of the users machine. For example, the above date object would display Sat Sep 23 2017 19:00:00 GMT-0700 (PDT) if the users machine was in San Francisco.

I have tried to manipulate this date in a variety of ways, including:

  1. Using moment.js for timezone manipulation and then calling .toDate() on the resulting moment. This discards all timezone information and brings us back to square one.
  2. Using variations of date.getTime() and date.getTimezoneOffset() to calculate the appropriate the milliseconds since epoch for the selected timezone. This also does not work as expected.
  3. Stripping the String entirely of it's timezone information. This results in the Date object interpreting the time incorrectly.

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

Orbit
  • 2,985
  • 9
  • 49
  • 106
  • @JayLane Unfortunately not. The selected answer suggests converting to UTC time throughout the application, which while a good suggestion, we cannot do at this time. The second suggestion is actually the same as the one I noted in item 2. – Orbit Sep 21 '17 at 20:40
  • did you try moment's timezone library? not sure if it's coupled in moment.js as well.http://momentjs.com/timezone/ – Jay Lane Sep 21 '17 at 20:45
  • Possible duplicate of [How to make JS date respect local timezone?](https://stackoverflow.com/questions/22814814/how-to-make-js-date-respect-local-timezone) –  Sep 21 '17 at 20:51
  • @JayLane Yes, I mentioned I tried moment in item 1. Timezone manipulation can be done in moment, but once the moment is converted to a JS `Date` object via moments `.toDate()` function, it loses its timezone information. – Orbit Sep 21 '17 at 21:25
  • Date objects don't have a timezone, they are always UTC. You can, however, adjust the output to represent the date and time in any particular timezone. – RobG Sep 21 '17 at 22:44
  • Are you expecting for someone in say, Chicago, to add 1 day and get `Sun, 24 Sep 2017 21:00:00 CDT -05:00`, then add 3 months and get `Sun, 23 Dec 2018 20:00:00 CST -06:00`? Not all places in the Central Timezone use CDT in summer. – RobG Sep 21 '17 at 23:12
  • For advanced date manipulations try using https://momentjs.com and https://momentjs.com/timezone. – Frane Poljak Sep 21 '17 at 23:46
  • Possible duplicate of [How do you create a JavaScript Date object with a set timezone without using a string representation](https://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s) – Vega Sep 24 '17 at 22:06

1 Answers1

0

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.

Place              CDT starts     CDT ends
Tabasco, Mexico:    2 Apr 2017    29 Oct 2017
Chicago, USA:      12 Mar 2017     5 Nov 2017
Easter Is, Chile:  12 Aug 2017    12 May 2018 <-- Southern hemisphere

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.

RobG
  • 142,382
  • 31
  • 172
  • 209