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

Lis*_* B. 3 timestamp google-sheets google-apps-script google-forms

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

像这样:

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('');
    }
  }
Run Code Online (Sandbox Code Playgroud)

Neo*_*Neo 9

在您的代码中,像这样获取脚本的时区:

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


Ala*_*lls 8

你需要使用:

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

文档中有一个例子:

Google文档 - formatDate

  • http://webapps.stackexchange.com/a/100644/27487 建议*不*使用 Utilities.formatDate()。 (2认同)