I have an use case where I use input from the user, which is on the form YYYY-MM-DD'T'TT:mm
to create a Date
object in Javascript. The problem is that Firefox and Chrome interpret the input as the local time (which is what I want), whilst Safari interprets the input as UTC time and converts it to local. How do I force Safari to use the same interpretation of the input as the other two?
Asked
Active
Viewed 46 times
0

Hedvilma
- 11
- 1
-
1You can't, you should not use the built-in parser to parse strings, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) You can write a simple parser for a single format in about 4 lines of code, or use a library. – RobG Aug 16 '18 at 12:58
1 Answers
0
The simplest way is to parse the string yourself (it's trivial, after all) and use the multiple-argument Date
constructor, which always works in local time. Or use a library like Moment.js to do it for you. I was tempted to suggest adding a timezone offset (+0400
, etc.) to the string (based on getTimezoneOffset
on a Date
object), but determining the right timezone offset requires that you know the date/time (because of Daylight Savings Time), and so...you'd have to do the work anyway.

T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875