How to Get Today’s date in the Google Apps Script – Definitive Guide

Google Apps script allows you to build your own web applications that integrate with Google Workspace.

You can get today’s date in the Google apps script using the new Date() object.

This tutorial teaches you how to get today’s date in the Google apps script.

Get Todays Date using new Date() method

The Date object represents a single moment in time. The new Date() object returns a date object with the current date and time of the current time zone.

To get the current date in the Google apps script,

  • Initialise a new Date() object and use it directly as needed.

Code

    var now = new Date();
    console.log(now);

Output

Wed Mar 01 2023 17:37:31 GMT+0530 (India Standard Time)

Handling TimeZones While Getting Date

The time zone for the spreadsheet is defined in the Timezone property that can be customised in the File -> Settings option.

Defining Timezone property in the spreadsheet
Defining Timezone property in the spreadsheet

When you use any time or date-related utility method,

  • The value will be returned in the timezone defined in this property. For example, if the timezone specified here is GMT+0530 (India Standard Time), you’ll get the current date and time in the GMT+0530 timezone.

To get the current date and time at a different timezone as a date object,

  • Update the timezone at the spreadsheet level.

Alternatively, as demonstrated below, you can convert the date object to a different locale string. The date object will be converted into a String type instead of the Date type.

 var now = new Date().toLocaleString('en', {timeZone: 'America/New_York'});

 Logger.log(now);

Output

The date at the specific time zone is displayed in a String format.

3/1/2023, 7:27:18 AM

Formatting the Date Object

To get today’s date and format in a different string format,

  • Use the Utilities.formatDate() method
  • When you use this method, the date will be returned in a String format instead of a Date object format

The formatDate() method accepts three parameters.

  • date – The date object to format, use new Date() to format the current date
  • Timezone – The output timezone of the resultant string
  • date Format – Format of the output string as per the SimpleDateFormat

Code

  var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
  Logger.log(formattedDate);

Output

2023-03-01 12:10:19

Additional Resources

Leave a Comment