我遇到一些非常简单的问题 - 只需在Google Apps脚本(javascript)中创建一个Date()
var thisdate = new Date('2017-02-12');
Logger.log(thisdate.toString());
此测试导致“无效日期”...我必须遗漏一些非常明显的内容!?
由于
答案 0 :(得分:1)
不幸的是,“2017-02-12”格式的日期字符串在Apps脚本中不起作用,即使 是有效的JavaScript日期串。你可以用斜线替换破折号,它会起作用。
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)
答案 1 :(得分:1)
您可以将以逗号分隔的数字作为Date对象构造函数的参数传递。
var d = new Date(2017, 2, 12); //year, month, date.
月份从0开始,所以2实际上是3月。在下面的示例中,月份的输出将为3
Logger.log(Utilities.formatDate(d, timezone, "dd-MM-yyyy"));