Convert a string to date in google apps script

You can use moment.js to parse a string of date in GAS.

First you need to add moment.js to your GAS. In script editor, go to "Resources" then "Libraries...", then put this project key MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 and click "Add". Choose the version from the dropdown, then click "Save".

Parsing date with moment is as easy as this.

var momentDate = Moment.moment("20140807", "YYYY/MM/DD");

What's returned here is a moment object, which is a wrapper of JavaScript's native Date object and very handy for date manipulation .

But, if you still need to get JavaScript's Date object, you can get it like this;

var date = momentDate.toDate();

You can also create formatted date time string with moment.js. Here is the reference for the tokens to specify format.


Be careful to pick the right timezone! In my example its

  • "+01:00 " => GMT+1 = Berlin (without daylight saving).

Other possible options

  • ".000000000Z" = "+00:00" => UTC = GMT = Unix Time
  • "-05:00" => EST = New York/ America
let date = "2021-03-10"
let time = "10:03:00"
let offset_to_utc = "+01:00"
let new_date_object = new Date(date + "T" + time + offset_to_utc)

The only way - parse the string by hand:

var dateStr = "20140807"

var year = +dateStr.substring(0, 4)
var month = +dateStr.substring(4, 6)
var day = +dateStr.substring(6, 8)

var pubdate = new Date(year, month - 1, day)

The reason for month - 1 is that the month numeration starts from 0.

And the + sign before function just converts string, which is returned by functions, to numbers so we can pass them right into new Date()