Get today date in Google Apps Script
164,721
Solution 1
Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
You can change the format by doing swapping the values.
- dd = day(31)
- MM = Month(12) - Case sensitive
- yyyy = Year(2017)
function changeDate() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
// You could use now Date(); on its own but it will not look nice.
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
var endDate = date
}
Solution 2
The Date
object is used to work with dates and times.
Date objects are created with new Date()
var now = new Date();
now - Current date and time object.
function changeDate() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
var date = new Date();
sheet.getRange(5, 2).setValue(date);
}
Solution 3
Google Apps Script is JavaScript, the date object is initiated with new Date()
and all JavaScript methods apply, see doc here
Solution 4
The following can be used to get the date:
function date_date() {
var date = new Date();
var year = date.getYear();
var month = date.getMonth() + 1; if(month.toString().length==1){var month =
'0'+month;}
var day = date.getDate(); if(day.toString().length==1){var day = '0'+day;}
var hour = date.getHours(); if(hour.toString().length==1){var hour = '0'+hour;}
var minu = date.getMinutes(); if(minu.toString().length==1){var minu = '0'+minu;}
var seco = date.getSeconds(); if(seco.toString().length==1){var seco = '0'+seco;}
var date = year+'·'+month+'·'+day+'·'+hour+'·'+minu+'·'+seco;
Logger.log(date);
}
Solution 5
function myFunction() {
var sheetname = "DateEntry";//Sheet where you want to put the date
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
// You could use now Date(); on its own but it will not look nice.
var date = Utilities.formatDate(new Date(), "GMT+5:30", "yyyy-MM-dd");
//var endDate = date;
sheet.getRange(sheet.getLastRow() + 1,1).setValue(date); //Gets the last row which had value, and goes to the next empty row to put new values.
}
Related videos on Youtube
Author by
user3347814
Updated on April 11, 2022Comments
-
user3347814 about 2 years
How do I get the Today date on google appscript?
I need to write a code to input today´s date in a cell.
function changeDate(){ var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG); var date = //Today´s date!?!?!!? var endDate = date; sheet.getRange(5, 2).setValue(endDate); }
-
Serge insas over 6 yearsNot really a good idea, the value returned by
Utilities.formatDate()
is a string, not a date object...you won't be able to do anything with it... to change the display format in the cell while keeping the date object properties use setNumberFormat(numberFormat) for full explanation and this doc as well -
Serge insas over 6 yearsthis is a very bad idea, you are converting the date to a string then you loose all the date object properties in the sheet cell. And obviously you took this from a script you have that contains unnecessary parameters such as sheet name...
-
Vignesh R over 6 yearsI have updated the script. Now I am not converting date object to string. I am getting exact date as 11/27/2017 11:05:01 in the cell. But previous version of code with converting date object to string. I got full string of Mon Nov 27 2017 11:04:46 GMT+0530 (IST) of date. I didn't get any unnecessary parameters like sheet name as you mentioned. Kindly do correct me if I any thing wrong.
-
EnriqueBet almost 4 yearsPlease add some description about your answer, consider adding details related to a specific line or lines of code that you changed and why they need to be changed. Thanks!
-
Noor Hossain over 3 yearsso, @Sergeinsas how to get today's date object ?
-
Serge insas over 3 yearsjust like that : var date = new Date();
-
PhilHibbs over 2 yearsHow would I set it to just the date, rather than the date and time? What's the best way of removing the time element?