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"... What am I doing wrong? ANY help would be greatly appreciated.
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);
}
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"... What am I doing wrong? ANY help would be greatly appreciated.
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);
}
Share
edited Apr 3, 2015 at 20:12
KyleMit♦
30.7k72 gold badges510 silver badges702 bronze badges
asked Aug 9, 2012 at 2:32
user1586374user1586374
411 gold badge1 silver badge3 bronze badges
3 Answers
Reset to default 3I 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 es 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 :-)
I suggest review this:
https://developers.google./apps-script/reference/utilities/utilities?hl=es#formatDate(Date,String,String)
And the format patterns are these:
http://docs.oracle./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")
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