Javascript 时间戳日期&;时间格式-应用程序脚本-格式日期和时间

Javascript 时间戳日期&;时间格式-应用程序脚本-格式日期和时间,javascript,datetime,google-apps-script,google-sheets,triggers,Javascript,Datetime,Google Apps Script,Google Sheets,Triggers,我无法在电子表格单元格中获取包含时间的时间戳格式。目前,它只生成日期。我需要更改什么才能获得同时显示日期和时间的时间戳 像这样: 2015年3月17日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 Col

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

像这样:

2015年3月17日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('');
    }
  }
您需要使用:

var formattedDate = Utilities.formatDate(time, "GMT", "MM-dd-yyyy HH:mm:ss");
文档中有一个示例:


在代码中,按如下方式获取脚本的时区:

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

建议不要使用Utilities.formatDate()。绝对有理由不将电子表格单元格格式更改为文本,并将日期另存为文本。