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/

Rubén
  • 34,714
  • 9
  • 70
  • 166
citizen conn
  • 15,300
  • 3
  • 58
  • 80

2 Answers2

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").

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 1
    That did it. Is there a reason why JSFiddle is able to parse it but Google Apps Script cannot? – citizen conn Jul 13 '11 at 18:57
  • 6
    @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. – Mike Samuel Jul 13 '11 at 19:00
  • 1
    My reading of [section 15.9.1.15](https://es5.github.io/x15.9.html#x15.9.1.15) is that milliseconds could be omitted; the absent value should be interpreted as zero. –  Aug 13 '15 at 01:48
  • 1
    @NormalHuman, You're referring to the "one of the following time forms" verbiage? You're probably right then. Rhino was being overly strict. – Mike Samuel Aug 13 '15 at 12:55
  • 2
    "was being overly strict" => "is being overly strict." 4 years later, this is still an issue. Thanks for the answer! – Bryan Sep 30 '15 at 00:02
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);
Howdy
  • 557
  • 7
  • 12