admin管理员组

文章数量:1404950

I have a time format I get from Google Calendar events, and i need to convert it to be readable in Google Timeline Charts (using Google Apps Scripts). I would've thought it would use the same format but it doesn't.


I need this: "Tue Oct 01 11:30:00 GMT+10:00 2019"

To resemble this: "new Date(2019, 10, 01, 11, 30)"


Here is what I have so far, but the problem is Apps Script can't find 'DateTime' ("ReferenceError: "DateTime" is not defined.")

Any thoughts on how to load DateTime or another method to convert?


var result = DateTime.ParseExact(
    "Tue Oct 01 11:30:00 GMT+10:00 2019", 
    "ddd mmm dd hh:mm:00 \\\\\\\\\ yyyy+", 
    CultureInfo.InvariantCulture)
    .ToString("yyyy+, mm, dd, hh, mm, ss");

Logger.log(result);

I have a time format I get from Google Calendar events, and i need to convert it to be readable in Google Timeline Charts (using Google Apps Scripts). I would've thought it would use the same format but it doesn't.


I need this: "Tue Oct 01 11:30:00 GMT+10:00 2019"

To resemble this: "new Date(2019, 10, 01, 11, 30)"


Here is what I have so far, but the problem is Apps Script can't find 'DateTime' ("ReferenceError: "DateTime" is not defined.")

Any thoughts on how to load DateTime or another method to convert?


var result = DateTime.ParseExact(
    "Tue Oct 01 11:30:00 GMT+10:00 2019", 
    "ddd mmm dd hh:mm:00 \\\\\\\\\ yyyy+", 
    CultureInfo.InvariantCulture)
    .ToString("yyyy+, mm, dd, hh, mm, ss");

Logger.log(result);
Share asked Sep 25, 2019 at 2:28 JackNapierJackNapier 4482 gold badges6 silver badges19 bronze badges 1
  • 1 In ECMAScript, months are zero indexed so for October you want new Date(2019, 9, 01, 11, 30). ;-) – RobG Commented Sep 25, 2019 at 4:21
Add a ment  | 

1 Answer 1

Reset to default 2

On Google Apps Script, to format a date as a string use Utilities.formatDate(...)

DateTime.ParseExact(...) isn't standard JavaScript. You could declare it before using it, or just use the above suggestion.

本文标签: dateConvert DateTime string using Javascript and Apps ScriptStack Overflow