Google脚本格式当输入日期为12月30日或12月31日时,日期为一年

时间:2018-08-18 02:07:52

标签: google-apps-script

在使用Utilities.formatDate时,似乎在处理12月30日或12月31日时要增加一年。我创建了此代码段来说明问题:

function myFunction() {
  start = new Date("29 Dec 2018")
  for(i=0;i<4;i++){
    Logger.log("Test:" + start.getDate() + "/" + (start.getMonth()+1) + "/" + start.getYear())
    test(start)
    start.setDate(start.getDate()+1)
  }
}

function test(SrcDate) {
  Logger.log("SrcDate: " + SrcDate)
  Logger.log("toISOString: " + SrcDate.toISOString())
  Logger.log("Zulu: " + Utilities.formatDate(SrcDate,"Zulu", "dd MMM YYYY HH:mm"))
  Logger.log("New York: " + Utilities.formatDate(SrcDate,"America/New_York", "dd MMM YYYY HH:mm"))
  Logger.log("SameZone: " + Utilities.formatDate(SrcDate,Session.getScriptTimeZone(), "dd MMM YYYY HH:mm")+ " -- " + Session.getScriptTimeZone())
}

这是通过运行myFunction()生成的日志:

[18-08-17 18:59:42:045 PDT] Test:29/12/2018
[18-08-17 18:59:42:046 PDT] SrcDate: Sat Dec 29 2018 00:00:00 GMT-0800 (PST)
[18-08-17 18:59:42:047 PDT] toISOString: 2018-12-29T08:00:00.000Z
[18-08-17 18:59:42:048 PDT] Zulu: 29 Dec 2018 08:00
[18-08-17 18:59:42:049 PDT] New York: 29 Dec 2018 03:00
[18-08-17 18:59:42:051 PDT] SameZone: 29 Dec 2018 00:00 -- America/Los_Angeles

[18-08-17 18:59:42:052 PDT] Test:30/12/2018
[18-08-17 18:59:42:053 PDT] SrcDate: Sun Dec 30 2018 00:00:00 GMT-0800 (PST)
[18-08-17 18:59:42:053 PDT] toISOString: 2018-12-30T08:00:00.000Z
[18-08-17 18:59:42:054 PDT] Zulu: 30 Dec 2019 08:00
[18-08-17 18:59:42:055 PDT] New York: 30 Dec 2019 03:00
[18-08-17 18:59:42:056 PDT] SameZone: 30 Dec 2019 00:00 -- America/Los_Angeles

[18-08-17 18:59:42:057 PDT] Test:31/12/2018
[18-08-17 18:59:42:057 PDT] SrcDate: Mon Dec 31 2018 00:00:00 GMT-0800 (PST)
[18-08-17 18:59:42:058 PDT] toISOString: 2018-12-31T08:00:00.000Z
[18-08-17 18:59:42:059 PDT] Zulu: 31 Dec 2019 08:00
[18-08-17 18:59:42:059 PDT] New York: 31 Dec 2019 03:00
[18-08-17 18:59:42:061 PDT] SameZone: 31 Dec 2019 00:00 -- America/Los_Angeles

[18-08-17 18:59:42:061 PDT] Test:1/1/2019
[18-08-17 18:59:42:062 PDT] SrcDate: Tue Jan 01 2019 00:00:00 GMT-0800 (PST)
[18-08-17 18:59:42:062 PDT] toISOString: 2019-01-01T08:00:00.000Z
[18-08-17 18:59:42:063 PDT] Zulu: 01 Jan 2019 08:00
[18-08-17 18:59:42:064 PDT] New York: 01 Jan 2019 03:00
[18-08-17 18:59:42:065 PDT] SameZone: 01 Jan 2019 00:00 -- America/Los_Angeles

我在这里做什么错了?

1 个答案:

答案 0 :(得分:2)

Y用作工作日。因此,请修改为y

参考文献: