Scriptdrill

Getting started Updated November 2026

Utilities.formatDate

Utilities.formatDate(date, timeZone, pattern) turns a Date into text: Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd") gives 2026-11-03. Patterns use Java letters: yyyy year, MM month, dd day, HH hour (24h), mm minute, ss second, EEEE weekday name, MMMM month name. MM is month and mm is minutes.

Syntax

Utilities.formatDate(date, timeZone, format)
PartWhat it is
dateA JavaScript Date object (not a string)
timeZoneIANA zone like "America/New_York", "Europe/Amsterdam", "GMT", or Session.getScriptTimeZone()
formatJava SimpleDateFormat pattern; literal text goes in single quotes

Examples

const tz = Session.getScriptTimeZone();
Logger.log(Utilities.formatDate(new Date(2026, 10, 3, 9, 5), tz, "yyyy-MM-dd HH:mm"));

Result: 2026-11-03 09:05

Logger.log(Utilities.formatDate(new Date(2026, 10, 3), tz, "EEEE, d MMMM yyyy"));

Result: Tuesday, 3 November 2026

const sheet = SpreadsheetApp.getActiveSheet();
const dates = sheet.getRange("A2:A4").getValues();
const text = dates.map(r => [Utilities.formatDate(r[0], tz, "dd/MM/yyyy")]);
sheet.getRange("B2:B4").setValues(text);

Result: Column B holds the dates from column A as text like 03/11/2026

const name = "Report_" + Utilities.formatDate(new Date(), "GMT", "yyyyMMdd'T'HHmmss'Z'") + ".pdf";
Logger.log(name);

Result: Report_20261103T140512Z.pdf

Common mistakes

  • Months show as minutes (2026-05-03 for November): lowercase mm is minutes; use MM for month.
  • The parameter (class java.lang.String) does not match: you passed a string instead of a Date; wrap with new Date(value) or read the cell as a Date.
  • The date is one day off: the time zone is GMT while the sheet is in another zone; pass the spreadsheet's zone, SpreadsheetApp.getActive().getSpreadsheetTimeZone().
  • Cannot read properties of null for a blank cell: getValues gives an empty string for blanks; check value instanceof Date before formatting.

Practice Utilities.formatDate

These drills check your answer on live data, free, no account needed.

  • Format a durationFormat a whole number of seconds like `"1d 2h 3m 4s"`, using days, hours, minutes and seconds, largest first. Leave out units that are zero. Zero seconds is `"0s"`.
  • Add days to a date`isoDate` is a calendar date like `"2024-01-30"`. Return the date `n` days later (earlier when `n` is negative) in the same `YYYY-MM-DD` form. Work in UTC so the answer does not depend on the time zone of the computer, and get leap years right.
  • Last day of the monthReturn the number of the last day of a month (28, 29, 30 or 31). `month` runs from 1 for January to 12 for December. Leap years follow the Gregorian rules: every 4 years, except century years, unless divisible by 400.

Questions

How do I show a 12-hour time with AM and PM?

Use h and a: Utilities.formatDate(d, tz, "h:mm a") gives 2:05 PM.

Can I format a date without Utilities?

Yes with toLocaleDateString in V8, but Utilities.formatDate gives you explicit control of the zone and pattern, which is what sheets usually need.

Related

  • Dates in Apps ScriptHandle dates in Google Apps Script: sheet dates arrive as Date objects, create dates with new Date(y, m, d), add days, days between, compare, time zone.
  • Create calendar eventsCreate Google Calendar events from a sheet with Apps Script: CalendarApp.createEvent with start and end dates, all-day events, guests, avoiding duplicates.
  • String methodsString methods you need in Apps Script: trim, toLowerCase, split, join, includes, startsWith, replace with regex, slice, padStart and template literals.
  • Create a Google Doc from a templateGenerate Google Docs from a template with Apps Script: copy the template with DriveApp, replace {{placeholders}} with sheet values via DocumentApp, make PDFs.

Previous: Logger.log and console.logNext: try, catch and finally