Hope all of you are doing fine. I stumbled across a problem in SAFARI. If I have a date like this
2016-05-31T10:00:00
and apply this function to the date
new Date(2016-05-31T10:00:00)
it returns me a proper result
Thu Mar 31 2016 10:00:00 GMT+0500 (PKT)
but if I add timezone offset to my date
2016-05-31T10:00:00-0400
and then apply the new Date
function on it, it starts throwing an invalid date
error. Can somebody tell me why is this happening and whats the way to resolve this problem
The problem is that ECMA-262 defines the timezone component as being ±HH:mm, so "-0400" is missing the separating colon.
Since the string is not compliant with the standard, the parser has the option of applying its own logic, or returning an invalid Date. Safari applies the latter logic.
The general advice is to not use the built-in parser and either use a library or to implement your own parser for your particular format (not difficult, only 4 or 5 lines of code).