Google Sheets Script Date Form

Paul Konstanski
Paul Konstanski used Ask the Experts™
I am using a prewritten script in Google Sheets.

It reads the column titles and if it encounters a column title named "Timestamp" it posts the current Timestamp. Here is that portion of the script.

    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push((e.parameter[headers[i]] && e.parameter[headers[i]]!='undefined') ? e.parameter[headers[i]] : '');

Open in new window

It places the current Date into the row for that column in the format 9/28/2019 8:42:49.

I would like it to place the format as: 2019-09-28 08:42:49.  

Is that possible?

I tried this and it didn't work.
row.push(new Date("Y-m-d H:i:s"));

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Prepress/OMS Specialist
This would be best accomplished by simply formatting that column in the format that you want it to appear.  That way you won't need to update your script when you want to change the date format the next time.

You can format the column by selecting the column and then clicking on the toolbar option that displays "123" with a down arrow next to it.  You should be able to simply select Date -> Date time, but if that isn't what you are looking choose More Formate -> Custom number format, and you should be able to specify the format there as you wish.

Let me know if you still have questions on this.
Paul KonstanskiProject Specialist


Thanks for confirming what I had pretty much concluded. I now have it working as I desire.
Jim RiddlesPrepress/OMS Specialist

Glad to hear you resolved your question.  Have a great day!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial