google apps script format date string

To format a date string in Google Apps Script, you can use the Utilities.formatDate() method. This method allows you to convert a date object to a formatted string using a specified format.

The syntax for Utilities.formatDate() is as follows:

formatDate(date, timeZone, format)
  • date: The date object that you want to format.
  • timeZone: The time zone in which you want to display the formatted date. This is an optional parameter and can be omitted if you want to use the default time zone.
  • format: The format string specifying how you want the date to be formatted.

Here's an example of how you can use Utilities.formatDate() to format a date string:

var date = new Date();
var formattedDate = Utilities.formatDate(date, "GMT", "yyyy-MM-dd");

In this example, date represents the current date and time. The formatDate() method is used to format date in the format "yyyy-MM-dd". The resulting formatted date string is stored in the formattedDate variable.

You can customize the format string according to your desired output. The format string uses different placeholders to represent different parts of the date. For example, "yyyy" represents the four-digit year, "MM" represents the two-digit month, and "dd" represents the two-digit day.

I hope this helps! Let me know if you have any further questions.