Reputation: 358
I have a string "2014-01-27". Now how can it be converted into "2014-01-27T00:00:00.0000000" ? Is that a valid date format avail in javascript ?
Upvotes: 0
Views: 42
Reputation: 147523
"2014-01-27T00:00:00.0000000" ? Is that a valid date format avail in javascript ?
Yes, but…
ECMA-262 says that where an ISO 8601-like string is missing the time zone (e.g. 2014-01-27T00:00:00.0000000) then it is assumed to be UTC. However, if you pass a string like that to Date.parse, some browsers treat it as local and some as UTC (and some, like IE 8, won't parse it at all, even with a time zone).
To avoid that, manually parse the string and create a UTC date:
function parseYMDasUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0],--b[1],b[2]));
}
console.log(parseYMDasUTC("2014-01-27")); // Mon Jan 27 2014 08:00:00 GMT+0800
Upvotes: 2