10

Does Google Apps Script use a funky version of EcmaScript that can't parse a date? How can I parse the date 2011-04-11T19:25:40Z into a JavaScript Date Object in Google Apps Script?

My log output from below logs NaN.

function showDate(){
  var d = Date.parse("2011-04-11T19:25:40Z");
  Logger.log(d); // <-- Logs NaN
}

Edit: http://jsfiddle.net/UTrYm/

2 Answers 2

15

The format specified in section 15.9.1.15 is YYYY-MM-DDTHH:mm:ss.sssZ so maybe try adding milliseconds to your date format as in Date.parse("2011-04-11T19:25:40.000Z").

Sign up to request clarification or add additional context in comments.

5 Comments

That did it. Is there a reason why JSFiddle is able to parse it but Google Apps Script cannot?
@citizen conn, I believe Google Apps Script uses Rhino to interpret your JavaScript, whereas JSFiddle is using the interpreter built into your browser. Rhino is allowed to reject that input according to the spec but your browser's interpreter is being more permissive than the spec requires and not requiring milliseconds.
My reading of section 15.9.1.15 is that milliseconds could be omitted; the absent value should be interpreted as zero.
@NormalHuman, You're referring to the "one of the following time forms" verbiage? You're probably right then. Rhino was being overly strict.
"was being overly strict" => "is being overly strict." 4 years later, this is still an issue. Thanks for the answer!
0

Google apps script works fine when you use slashes instead of dashes. Like:

var date = new Date ('2017/12/26 9:55 am');
Logger.log(date);

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.