时间戳日期&时间格式 - 应用脚本 - 格式日期和时间

时间:2015-03-17 15:32:45

标签: google-apps-script google-sheets timestamp google-form

我无法在电子表格单元格中获取时间戳格式以包含时间。目前,它只生成日期。我需要更改什么才能获得显示日期和时间的时间戳?

像这样:

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('');
    }
  }

2 个答案:

答案 0 :(得分:8)

您需要使用:

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

文档中有一个例子:

Google Documentation - formatDate

答案 1 :(得分:2)

在您的代码中,按如下所示获取脚本的时区:

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