0

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 ?

1
  • 2
    new Date("2014-01-27").toISOString() is at least close, showing milliseconds instead of nanoseconds. Commented May 14, 2014 at 8:54

1 Answer 1

2

"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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.