Google Apps脚本中的格式化日期

时间:2012-08-09 02:32:49

标签: javascript datetime google-apps-script

我在提交表单时试图获得一个包含简单格式日期的电子表格,但是所有日期(包括时间戳都会一直发布为" Dec 31 1969 2:00 PM" ...我究竟做错了什么?非常感谢任何帮助。

function formSubmitReply(e) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = sheet.getActiveRange().getRowIndex();
  var lastRow = sheet.getLastRow();
  // Set the status of a new ticket to 'New'.

  sheet.getRange(lastRow, getColIndexByName("Status")).setValue("New");

  var ticketTime = sheet.getRange(row, getColIndexByName("Timestamp")).getValue();

  var subdate = Utilities.formatDate(new Date(ticketTime), "GMT-10", "EEE MM/dd/yyyy 'at' h:mm a");
  sheet.getRange(lastRow, getColIndexByName("Timestamp")).setValue(subdate);

  var sDate = sheet.getRange(row, getColIndexByName("Start Date")).getValue();
  var strtdate = Utilities.formatDate(new Date(sDate), "GMT-10", "EEE, MMM dd, yyyy");
  sheet.getRange(lastRow, getColIndexByName("Start Date")).setValue(strtdate);

  var sTime = sheet.getRange(row, getColIndexByName("Start Time")).getValue();
  var strttime = Utilities.formatDate(new Date(sTime), "GMT-10", "h:mm");
  //sheet.getRange(lastRow, getColIndexByName("Start Time")).setValue(strttime);

  var eDate = sheet.getRange(row, getColIndexByName("End Date")).getValue();
  var enddate = Utilities.formatDate(new Date(eDate), "GMT-10", "EEE, MMM dd, yyyy");
  sheet.getRange(lastRow, getColIndexByName("End Date")).setValue(enddate);

  var eTime = sheet.getRange(row, getColIndexByName("End Time")).getValue();
  var endtime = Utilities.formatDate(new Date(eTime), "GMT-10", "h:mm");
  //sheet.getRange(lastRow, getColIndexByName("End Time")).setValue(endtime);
}

3 个答案:

答案 0 :(得分:3)

我认为电子表格中的'日期'不是日期对象,它们只是字符串而JavaScript会将其评估为0,这就是为什么你得到'origin'(又名epoch)的日期值减去GMT偏移...根据这些值在细胞中的来源,可能会有不同的解决方案。你能告诉他们如何创建示例数据吗?

要检查我的猜测是否正确,您只需尝试使用电子表格界面更改“日期”单元格的显示格式。如果您可以在那里更改并获得连贯的结果,那么我错了......如果没有......好吧我想我是对的:-)

答案 1 :(得分:3)

我建议对此进行审核:

https://developers.google.com/apps-script/reference/utilities/utilities?hl=es#formatDate(Date,String,String)

格式模式如下:

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

一个帮助你的例子

Utilities.formatDate(thisFile.getLastUpdated()," GMT-5"," yyyy / MM / dd,HH:mm:ss")

答案 2 :(得分:1)

此代码采用带有“Timestamp”的第一行的日期,即new Date ("TimeStamp"),因此当getRowIndex返回1时,它返回错误的日期:

function formSubmitReply(e) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = sheet.getActiveRange().getRowIndex();
  //try to increment row as below to get row 2
  row++;
  var lastRow = sheet.getLastRow();
  //increment last row to write to new row to observe new date
  lastRow++;
  // Set the status of a new ticket to 'New'.

  sheet.getRange(lastRow, getColIndexByName("Status")).setValue("New");

  var ticketTime = sheet.getRange(row, getColIndexByName("Timestamp")).getValue();

  var subdate = Utilities.formatDate(new Date(ticketTime), "GMT-10", "EEE MM/dd/yyyy 'at'   h:mm a");
  //use log window select views-> logs to see your output
  Logger.log("subdate : "+subdate);
  sheet.getRange(lastRow, getColIndexByName("Timestamp")).setValue(subdate);

  var sDate = sheet.getRange(row, getColIndexByName("Start Date")).getValue();
  var strtdate = Utilities.formatDate(new Date(sDate), "GMT-10", "EEE, MMM dd, yyyy");
  sheet.getRange(lastRow, getColIndexByName("Start Date")).setValue(strtdate);

  var sTime = sheet.getRange(row, getColIndexByName("Start Time")).getValue();
  var strttime = Utilities.formatDate(new Date(sTime), "GMT-10", "h:mm");
  //sheet.getRange(lastRow, getColIndexByName("Start Time")).setValue(strttime);

  var eDate = sheet.getRange(row, getColIndexByName("End Date")).getValue();
  var enddate = Utilities.formatDate(new Date(eDate), "GMT-10", "EEE, MMM dd, yyyy");
  sheet.getRange(lastRow, getColIndexByName("End Date")).setValue(enddate);

  var eTime = sheet.getRange(row, getColIndexByName("End Time")).getValue();
  var endtime = Utilities.formatDate(new Date(eTime), "GMT-10", "h:mm");
  //sheet.getRange(lastRow, getColIndexByName("End Time")).setValue(endtime);
}

//enter code here