2

In my browser console I'm able to parse dates and get their time in milliseconds:

new Date('2018-10-16 12:41:50.000000');
// => Tue Oct 16 2018 12:41:50 GMT+0200 (Central European Summer Time)
new Date('2018-10-16 12:41:50.000000').getTime();
// => 1539686510000

If I do the same in App scripts, the date is not recognized (defaults to 1970 epoch origin) and getTime() simply breaks (NaN):

function test_date() {
  Logger.log('new Date(date)');
  Logger.log(new Date('2018-10-16 12:41:50.000000'));
  Logger.log('getTime()');
  Logger.log(new Date('2018-10-16 12:41:50.000000').getTime());
}

enter image description here
enter image description here

Am I missing something or are new Date() / getTime() simply broken in App Scripts?

I'm runnign the above script in a Google Sheet

Rubén
  • 34,714
  • 9
  • 70
  • 166
Panda Coder
  • 157
  • 1
  • 9
  • Be super careful parsing dates with timezone not specified. I've seen Chrome assume the local timezone of the device and safari forcing UTC. Nightmare. If you don't know the timezone, it's safest to force UTC both serializing and deserializing, at least that way you get consistent behaviour and your dates don't magically increment. – bbsimonbb Oct 19 '18 at 08:12
  • Possible duplicate of [Why does my Date object in Google Apps Script return NaN](https://stackoverflow.com/questions/6683872/why-does-my-date-object-in-google-apps-script-return-nan) – Rubén Oct 19 '18 at 16:15

3 Answers3

3

Google Apps Script uses RFC3339. So 2018-10-16 12:41:50.000000 cannot be directly parsed by new Date('2018-10-16 12:41:50.000000'). By this, new Date('2018-10-16 12:41:50.000000') is the same with new Date(''), and both results become Thu Jan 01 01:00:00 GMT+01:00 1970. In order to parse the date, how about modifying to the format of RFC3339?

From:

Logger.log(new Date('2018-10-16 12:41:50.000000'));
Logger.log(new Date('2018-10-16 12:41:50.000000').getTime());

To:

Logger.log(new Date('2018-10-16T12:41:50.000Z'));
Logger.log(new Date('2018-10-16T12:41:50.000Z').getTime());

Result:

Tue Oct 16 12:41:50 GMT+02:00 2018
1.53968651E12

Note:

  • In your case, at Google Apps Script, the time zone of the script is reflected to the result. The time zone for this situation can be seen at Script Editor.
    • On script editor.
      • File -> Project properties -> Info -> Time zone
    • You can also confirm it by Logger.log(Session.getScriptTimeZone()) of the script.
  • new Date('2018-10-16T12:41:50') can be also parsed.

References:

If this was not what you want, I'm sorry.

Community
  • 1
  • 1
Tanaike
  • 181,128
  • 11
  • 97
  • 165
0

Instead of this

  Logger.log(new Date('2018-10-16 12:41:50.000000').getTime());

use this

  Logger.log((new Date('2018-10-16 12:41:50.000000')).getTime());
Atishay Jain
  • 1,425
  • 12
  • 22
0

Am I missing something or are new Date() / getTime() simply broken in App Scripts?

At this time the Google Apps Script's and Chrome's JavaScript engines aren't the same. By the other hand, each executes scripts in different contexts as Google Apps Script runs scripts on Google servers while Chrome runs scripts on the user computer.

I make emphasis on "at this time" because on Google I/O 2018 was announced that Google Apps Script will be running on V8.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166