在Google App Script中考虑以下日期格式代码
function testDateFormatter(){
// Sun Dec 31 2017 20:02:06
var date = new Date(1514746926811)
var dateString = Utilities.formatDate(date, "CET", "YYYY-MM-dd");
Logger.log("The date is : " + date + ", after formatting it is " + dateString)
}
结果是:
//
// The date is : Sun Dec 31 2017 20:02:06 GMT+0100 (CET), after formatting it is 2018-12-31
//
注意2018而不是2017 !!!!
为什么GAS将全年添加到日期?
对于其他样本日期,它可以正常工作,例如:
var date = new Date(1511377747255)
var dateString = Utilities.formatDate(date, "CET", "YYYY-MM-dd");
Logger.log("The date is : " + date + ", after formatting it is " + dateString)
//
// Logger: The date is : Wed Nov 22 2017 20:09:07 GMT+0100 (CET), after formatting it is 2017-11-22
//
答案 0 :(得分:-1)
我没有看到问题:
function myFunction() {
var dt=new Date(1514746926811);
var dts=Utilities.formatDate(dt, Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:ss")//not YYYY
Logger.log('dt=%s dts=%s',dt,dts);
}
记录器输出为:
[17-12-22 15:17:07:058 MST] dt = Sun Dec 31 12:02:06 GMT-07:00 2017 dts = 12/31/2017 12:02:06