Mathew Gellner-Garcia
Mathew Gellner-Garcia

Reputation: 15

Reformatting date from Google Sheets form

I'm using Google Forms to submit data into a Google Sheet. When I log the cell value which contains the date, it returns "Thu Apr 22 03:00:00 GMT-04:00 2021"

I want the date to look like "April 22, 2021"

Any advice on how to accomplish this? Any hints would be appreciated.

Thank you.

Upvotes: 0

Views: 36

Answers (1)

doubleunary
doubleunary

Reputation: 18784

Follow @MetaMan's advice and use Utilities.formatDate(). The timezone should be set to that of the dates you receive from the spreadsheet, because the script may be in a different timezone:

function logFormResponseDates() {
  const ss = SpreadsheetApp.getActive();
  const dates = ss.getSheetByName('Form Responses 1')
    .getRange('A2:A').getValues().flat().filter(String);
  const timezone = ss.getSpreadsheetTimeZone();
  const formattedDates = dates.map(date => Utilities.formatDate(date, timezone, 'mmmm d, yyyy'));
  console.log(JSON.stringify(formattedDates));
}

Upvotes: 2

Related Questions