How to get the current time in Google spreadsheet using script editor?

google-apps-script, google-sheets

Solution

use the JavaScript `Date()` object. There are a number of ways to get the time, date, timestamps, etc from the object. (Reference)

function myFunction() {
  var d = new Date();
  var timeStamp = d.getTime();  // Number of ms since Jan 1, 1970

  // OR:

  var currentTime = d.toLocaleTimeString(); // "12:35 PM", for instance
}

Problem

There is an option to write a script and bind it to a trigger. The question is, how to get the current time in the script body? ``` function myFunction() { var currentTime = // <<??? } ```

Original source