Get the time (correct time zone) using Google Spreadsheets

If you want to get the time in the correct time zone using Google Spreadsheets, first be sure to check the current spreadhseet time zone. Open your spreadsheet:

File > Settings for this spreadsheet > Time zone

Then:

javascript
// Declaring new date
var now = new Date();

// Getting the time zone
var timeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();

// Formatting the date
var date = Utilities.formatDate(now, timeZone, "dd/MM/yyyy HH:mm");

Logger.log(date);
// 31/07/2020 10:45

In one line of code:

javascript
var date = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "dd/MM/yyyy HH:mm");

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.