Lisa B.
Lisa B.

Reputation: 81

Timestamp Date & Time Format - Apps Script - Format Date and Time

I can't get the time-stamp format in a spreadsheet cell to also include the time. Currently, it only produces the date. What do I need to change to get the time-stamp to show both date and time?

Like this:

3/17/2015 10:57:45

function onEdit(event)
{
  var ss = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  if(r.getColumn() == 8){ //To check if update cell in Column, 1 means first column.
    if(r.getValue() == "Resolved"){ //To check the value is equal to Resolved
      ss.getRange('L'+r.getRow()).setValue(new Date()); //Set column B1 value to current date.
      date = Utilities.formatDate(time, "GMT", "HH:mm:ss");
    }else{
      ss.getRange('L'+r.getRow()).setValue('');
    }
  }

Upvotes: 8

Views: 55186

Answers (2)

Neo
Neo

Reputation: 817

In you code, get the time zone of the script like this:

var timeZone = Session.getScriptTimeZone();
date = Utilities.formatDate(new Date(), timeZone, "MM-dd-yyyy | HH:mm:ss");

Upvotes: 10

Alan Wells
Alan Wells

Reputation: 31300

You need to use:

var formattedDate = Utilities.formatDate(time, "GMT", "MM-dd-yyyy HH:mm:ss");

There is an example in the documentation:

Google Documentation - formatDate

Upvotes: 15

Related Questions