Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Date object in Google Apps Script return NaN

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/

like image 474
citizen conn Avatar asked Jul 13 '11 18:07

citizen conn


People also ask

How do you format a date in Google App script?

DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); String strDate = dateFormat. format(date);

How do you add days to a date in an app script?

getTime() + 24 * 60 * 60 * 1000); This adds the milliseconds in one day to testDate and creates a new Date from it. getTime() gets the representation of a date in milliseconds since the Unix epoch, so by adding the number of ms in one day to it, you can create a new Date that is +1 day.


1 Answers

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

like image 80
Mike Samuel Avatar answered Sep 20 '22 14:09

Mike Samuel