Skip to content

Avoid formatDate error in Google Apps Script

ive got a function to storage in an array and loop data from a document. Inside this one, there are cells with dates in format dd/mm/yyyy…but when I send it by email, appears like Wed Jan 01 2014 00:00:00 GMT-0300 (ART)

I used inside this function, a formatDate method but through me an error Cannot find method formatDate(string,string,string). How I can get the right formated date?

function getUsersExpDate(usersExpDate) {

  var expDateArray = [];

  var temp = usersExpDate[0];

  for(var n=0; n < usersExpDate.length; n++){

    expDateArray.push( usersExpDate[n] );    
    temp = usersExpDate[n];
    temp = Utilities.formatDate(temp, "GMT", "yyyy-MM-dd");

  }

  return expDateArray;

}

Answer

You need to convert the string to date first before calling the formatDate() method.

temp = new Date(usersExpDate[n]);
temp = Utilities.formatDate(temp, "GMT", "yyyy-MM-dd");