1

When using Utilities.formatDate it seems to be adding a year when processing December 30 or 31. I created this snippet to demonstrate the issue:

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())
}

This is the log generated from running 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

What am I doing wrong here?

Joe
  • 25,000
  • 3
  • 22
  • 44
  • Is this information useful for your situation? At the sample of [the document of Utilities.formatDate()](https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)), ``yyyy`` is used instead of ``YYYY``. You can see the Date and Time Patterns at [here](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). ``Y`` is used as Week year. – Tanaike Aug 18 '18 at 02:27
  • 1
    That was exactly it! YYYY gives the year that contains the week, yyyy is the year containing that day. Make that an answer so I can accept it. – Joe Aug 18 '18 at 02:30
  • I'm glad your issue was solved. I posted it as an answer. Could you please confirm it? – Tanaike Aug 18 '18 at 02:39

1 Answers1

3

Y is used as Week year. So please modify to y.

References :

Tanaike
  • 181,128
  • 11
  • 97
  • 165