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?
This worked for me, when converting date-string to date-object in Google Script.
The date-string was taken from the Google Sheet cell by getValues() method.
From:
01.01.2017 22:43:34
to:2017/01/01 22:43:34
did the job. And then thenew Date()
.The hard and sure shot way to make it work if the format is known beforehand is to parse it.
I haven't tested this exact piece of code, but have used similar code. So, this gives you a fair idea of how to proceed.
Found this other possible and very simple code that seems to also do the job :
Google Apps Script uses a particular version of JavaScript (ECMA-262 3rd Edition) and as you have discovered can't parse date/times in ISO 8601 format. Below is a modified function I use in a number of my Apps Scripts