Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('');
    }
  }
like image 747
Lisa B. Avatar asked Mar 17 '15 15:03

Lisa B.


2 Answers

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

like image 139
Alan Wells Avatar answered Oct 10 '22 23:10

Alan Wells


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");
like image 10
Neo Avatar answered Oct 11 '22 00:10

Neo