I'm having trouble with something that should be very simple - just creating a Date() in Google Apps Script (javascript)
var thisdate = new Date('2017-02-12');
Logger.log(thisdate.toString());
This test results in 'Invalid Date'... I must be missing something really obvious!?
Thanks
Unfortunately, a date string in the format of '2017-02-12' won't work in Apps Script, even though it should be a valid JavaScript date string. You can replace the dashes with slashes, and it will work.
var d,string;
string = '2017-02-12';
if (string.indexOf("-") !== -1) {//A dash was found in the date string
string = string.replace(/-/g,"/");//Replace dashes with slashes
Logger.log(string)
d = new Date(string);
} else {
d = new Date(string);
}
Logger.log(d)
You can pass the numbers, separated by comma, as arguments to the Date object constructor.
var d = new Date(2017, 2, 12); //year, month, date.
The months start with 0, so 2 is actually March. In the example below, the output for month will be 3
Logger.log(Utilities.formatDate(d, timezone, "dd-MM-yyyy"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.