Formatted dates in a Google Apps script

前端 未结 3 347
抹茶落季
抹茶落季 2021-01-13 21:59

I\'m trying to get a spreadsheet with simple formatted dates when a form is submitted, but all dates, including the timestamp keep getting posted as \"Dec 31 1969 2:00PM\"..

相关标签:
3条回答
  • 2021-01-13 22:12

    This code takes date of first row which carries "Timestamp", i.e. the new Date ("TimeStamp") so it's getting back the wrong date as getRowIndex return 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
    
    0 讨论(0)
  • 2021-01-13 22:19

    I think the 'dates' in your spreadsheet are not date objects, they are just strings and JavaScript evaluate it as 0, that's why you get a date value of the 'origin' (aka epoch) minus GMT offset...depending on how these values comes in the cells there might be different solutions to this. Can you show example data an tell how they were created?

    To check if my guess is right you could simply try to change the display format of the 'date' cells using the spreadsheet interface.If you can change it there and get coherent results then I'm wrong... if not ... well I guess I'm right :-)

    0 讨论(0)
  • 2021-01-13 22:32

    I suggest review this:

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

    And the format patterns are these:

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

    And one example to help you

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

    0 讨论(0)
提交回复
热议问题