22

I'm having some problems converting a string to a date object in google apps script.

My dates are in the following format, from a 3rd party API:

2013-01-17T17:34:50.507

I am attempting to convert this to a Date object:

return Date(stringDate);

And this is being returned:

Thu Jan 01 01:00:00 GMT+01:00 1970

Can someone tell me what i'm doing wrong, and how to resolve this issue ?

Sherlock
  • 5,557
  • 6
  • 50
  • 78

4 Answers4

12

With moment.js, it is as easy as this to parse any of ISO 8601 format.

var date = Moment.moment("2013-01-17T17:34:50.507").toDate();

You can use moment.js to parse your arbitrary date string as well.

To use moment.js in GAS, you just need to add it in the script editor. Open your script in GAS script editor and go to "Resources" then "Libraries...", then put this project key MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 and click "Add". Choose the version from the dropdown, then click "Save". Now, you are ready to use moment.js in GAS.

moment.js can be used to parse date string, create formatted date string, and many other date manipulation. Thanks to the author!

You can find the moment.js documentation here.

kanji
  • 729
  • 9
  • 14
  • +1 for Moment.js but you may prefer to embed your own than rely on a library. Case in point that, rightly, moment.js changes over time. e.g. deprecating particular date format strings it will work with may break your scripts, embedding it yourself allows you to manage the transition better than up-revving library version. – JSDBroughton May 13 '17 at 08:03
  • @Jonathon, thanks for your comment. Why don't we just stick with the library version specified when we add it to our script? So, we don't have to worry about the changes in the library. Am I missing your point? – kanji May 14 '17 at 00:33
  • You can. But, if a library is just a GAS wrapper to a .js library though I'd prefer to embed the source so you are in control of exactly which flavour you are using. Speed too. – JSDBroughton May 14 '17 at 00:45
8

It doesn't appear that the Date object knows how to handle that date. The date is in ISO 8601 format. Javascript can handle Dates if they are given timezone information.

You will have to do some testing, but if those dates given to you are in UTC time, then just add a Z to the end of the date string before you call new Date().

Edit: The Apps Script Date object can't seem to handle a timezone other than UTC when parsing a Date. I opened an issue for it.

Phil Bozak
  • 2,792
  • 1
  • 19
  • 27
  • I confirmed that the dates I am receiving are in GMT, and the environment I am in is in GMT also - I tried adding the Z and unfortunately the issue remains. The only way I can get the Date object to represent the right date is if I initialise using posix time for some reason, although this isn't an option here unfortunately. thanks for your assistance. – Sherlock Feb 13 '13 at 16:05
  • 5
    you should be calling `new Date(dateString)`, not just `Date(dateString)`. – Phil Bozak Feb 13 '13 at 16:07
5

It doesn't work in GScript, at least for me at the time I'm writing it. This post serves a working alternative: How do I format this date string so that google scripts recognizes it?

Community
  • 1
  • 1
user3310435
  • 51
  • 1
  • 1
  • Please don't just provide links in answer. Atleast provide a little summary to what is being explained in the link. – Rohan Kandwal Feb 14 '14 at 14:05
  • What happens if that StackOverflow answer is removed by its author for some reason? You should include at least an overview of the solution presented. – Chris Leyva Feb 14 '14 at 14:09
0

As of now new Date() seems to work:

var dT = new Date("2013-01-17T17:34:50.507");
console.info("dT: %s or %d", dT, dT.getTime());

returns dT: Thu Jan 17 17:34:50 GMT+01:00 2013 or 1.358440490507E12 in Google Apps Script

B.No
  • 85
  • 8