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?
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With