Get today date as title in of google sheet by google appScript

2.1k Views Asked by At

I'm currently working on a project where I need to set the title of a new google spreadsheet to the current time and date. the issue is that it's not giving me the correct time but the correct date and timezone. I need also to add a name alongside the current date and time.

function myFunction() {
SpreadsheetApp.create(new Date())
}
1

There are 1 best solutions below

0
On BEST ANSWER

You can use Utilities.formatDate(date, timeZone, format) to create a string date-time based on your preferred format and timezone.

After you create a date-time string, you can append your desired name using "+" operator.

Sample Code:

function createNewFile(){
  var dateTime = Utilities.formatDate(new Date(), "GMT+8", "yyyy-MM-dd'T'HH:mm:ss.SS");
  var ss = SpreadsheetApp.create("TestFile_"+dateTime);
}

Output File Name: TestFile_2021-02-13T03:05:09.807


Refer to this document to learn more on how you could format your date. Date time patterns were explained in the given reference, keep in mind that the letter is case sensitive, upper and lower case characters can give you different outputs/have different meanings.

Sample Pattern Format:

enter image description here