I want to convert 06/29/2017 10:13AM to a Javascript Date instance. Currently I split the time into 06/29/2017 and 10:13AM and converted 06/29/2017 using Date('06/29/2017').getTime(). I want to convert the time using the same way and adding the two together, but it isn't working. How should I go about doing this and is there a better way to do it?
Asked
Active
Viewed 41 times
0
-
1What's wrong with `Date("06/29/2017 10:13AM")` ? – Robert Lozyniak Jul 11 '17 at 22:34
-
Is there a reason you're calling `getTime`? – Matt Johnson-Pint Jul 11 '17 at 22:43
-
This question has been answered many, many times before. **Do not** use the built-in parser. – RobG Jul 11 '17 at 23:47
1 Answers
0
Assuming you simply want to convert it a Date-object, try using an UTC date:
new Date('2017-06-29T10:13:00+00:00'); // change to desired time zone

Zomry
- 1,141
- 12
- 14
-
That will actually be interpreted as local time (not UTC), under ISO8601 and the most recent version of the ECMAScript standard. Though I see no reason to not do it in local time. – Matt Johnson-Pint Jul 11 '17 at 22:42
-
You are right in that regard. However, you can still add a time zone (e.g. +05:00) to ensure that it converts to the right time zone. – Zomry Jul 11 '17 at 22:45
-
1@MattJohnson—except for browsers that don't follow ECMA-262 and treat `new Date('2017-06-29T10:13:00')` as UTC anyway, e.g. Safari. :-( – RobG Jul 11 '17 at 23:48