Search code examples
google-apps-scriptsimpledateformatdate-formatting

Utilities function in Google App Scripts not working


Function Utilities.formatDate in Google App Scripts is not working correct for date in year 2013

Example-

date = Tue Dec 31 2013 18:43:12 GMT+0530 (IST)

after formatting it in YYYYMMdd format

using code-

Utilities.formatDate(date, "IST" ,"YYYYMMdd"))

result was- 20**14**1231

In the above result year is expected to be 2013 as per above mentioned date.

The same code is working correct for date in 2012 and 2014.


Solution

  • Just change your pattern from YYYY to yyyy (lower case) and it will work, check this:

    function myFunction() {
      var date = new Date("Tue Dec 31 2013 18:43:12 GMT+0530 (IST)");
    
    //after formatting it in YYYYMMdd format
      var format = Utilities.formatDate(date,"IST", "yyyyMMdd");
    
      Logger.log(format);
    }