I’m working on a Phonegap-based iOS app, which is already done for Android. The following lines are working fine for Android but not for iOS. Why?
var d = new Date("2015-12-31 00:00:00");
console.log(d.getDate() + '. ' + d.getMonth() + ' ' + d.getFullYear();
Result for Android:
31.11 2015
Result on iOS:
NaN. NaN NaN
Where is the difference coming from?
Your date string is not in a format specified to work with
new Date. The only formats in the spec are a simplified version of ISO-8601 (added in ES5 in 2009 and updated in ES2015 and ES2016), and the format output byDate.prototype.toString. Your string isn’t in either format, but it’s really close to the ISO-8601-like format. It would also be easy to change it to a format that isn’t in the spec, but is universally supportedFour options for you:
Temporalfeature (it’s now at Stage 3)Use the upcoming
TemporalfeatureThe
Temporalproposal is at Stage 3 as of this update in August 2021. You can use it to parse your string, either treating it as UTC or as local time:Treating the string as UTC:
Or treating it as local time:
Temporaldoesn’t have the problem mentioned below that the specified date/time string format historically had when no timezone was specified.The ISO-8601-like format
If you change the space to a
T, you’ll be in spec:(I’m assuming you’re not actually using a string literal, hence the
replacecall.)For reliable timezone handling in old browsers, you’ll also want to append a
Z(for GMT/UTC) or a timezone indicator (+/-HH:MM), because the handling of strings without them was mis-specified in ES5, updated in ES2015, and then updated further in ES2016. Current versions of modern browsers follow the spec now, which says:(ES5 said always default to UTC. ES2015 said always default to local time. ES2016 is where the current behavior was defined. It’s been stable since.)
So it’s best to include a timezone indicator, especially if you have to support older browsers. Note that it must be a
Z(UTC) or+/-HH:MM; abbreviations likeCSTare not allowed, as there’s no standard for them. Here’s a UTC example:An unspecified format that’s near-universally supported
There’s a seconnd format that isn’t in the specification but is near-universally supported and has been for a long time:
YYYY/MM/DD HH:MM:SS, which is interpreted as local time. So:Again, though, that’s unspecified behavior, so caveat emptor. But it works in at least IE8+ (probably earlier), Chrome and anything else using the V8 JavaScript engine, Firefox, and Safari.
Parse it yourself
It’s also easy to parse that string yourself. Using ES2020+ features:
Or with only ES5-level features (since the question is from 2015):