Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format this date string so that google scripts recognizes it?

I'm getting a json feed with a date string formatted like the following:

//2012-08-03T23:00:26-05:00

I had thought I could just pass this into a new Date as such

var dt = new Date("2012-08-03T23:00:26-05:00");

This works on jsfiddle but not in google scripts. It's returning an invalid date. After reading this post I recognize it may be how GAS interprets the date string so now I'm not sure how to reformat the date to make it work.

Is there a best way to reformat that date string so GAS can recognize it as a date?

like image 427
jrad Avatar asked Aug 04 '12 17:08

jrad


2 Answers

As of now new Date() seems to work:

var dT = new Date("2012-08-03T23:00:26-05:00");
console.info("dT: %s or %d", dT, dT.getTime());

returns dT: Sat Aug 04 06:00:26 GMT+02:00 2012 or 1.344052826E12 in Google Apps Script

like image 153
B.No Avatar answered Sep 30 '22 17:09

B.No


Found this other possible and very simple code that seems to also do the job :

function test(){
  Logger.log(isoToDate("2013-06-15T14:25:58Z"));
  Logger.log(isoToDate("2012-08-03T23:00:26-05:00"));
  Logger.log(isoToDate("2012-08-03T23:00:26+05:00"));
}

function isoToDate(dateStr){// argument = date string iso format
  var str = dateStr.replace(/-/,'/').replace(/-/,'/').replace(/T/,' ').replace(/\+/,' \+').replace(/Z/,' +00');
  return new Date(str);
}
like image 43
Serge insas Avatar answered Sep 30 '22 16:09

Serge insas