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:
- 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. - Using variations of
date.getTime()
anddate.getTimezoneOffset()
to calculate the appropriate the milliseconds since epoch for the selected timezone. This also does not work as expected. - 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?