62

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);

 }
Rubén
  • 34,714
  • 9
  • 70
  • 166
user3347814
  • 1,138
  • 9
  • 28
  • 50

6 Answers6

87
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
}
Haytch
  • 977
  • 7
  • 6
  • 8
    Not 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)](https://developers.google.com/apps-script/reference/spreadsheet/range#setNumberFormat(String)) for full explanation and [this doc](https://developers.google.com/sheets/api/guides/formats) as well – Serge insas Oct 20 '17 at 16:37
22

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); 
}
Vignesh R
  • 579
  • 2
  • 7
  • 19
  • 1
    this 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... – Serge insas Nov 23 '17 at 11:02
  • I 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. – Vignesh R Nov 27 '17 at 05:38
  • so, @Sergeinsas how to get today's date object ? – Noor Hossain Dec 07 '20 at 13:55
  • just like that : var date = new Date(); – Serge insas Dec 08 '20 at 07:27
  • 1
    How would I set it to just the date, rather than the date and time? What's the best way of removing the time element? – PhilHibbs Jan 05 '22 at 10:27
9

Google Apps Script is JavaScript, the date object is initiated with new Date() and all JavaScript methods apply, see doc here

Serge insas
  • 45,904
  • 7
  • 105
  • 131
6

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);
}
4

Easiest way, you can use javascript date object which is new Date().

function changeDate(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(GA_CONFIG);
  var date =  new Date();

  sheet.getRange(5, 2).setValue(date);

 }

But then you will get the whole time object. You just need to change the format in spreadsheet to time or date, whatever you like.

Asyraf Syahmi
  • 127
  • 1
  • 1
  • 7
3
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.
}
  • 2
    Please 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! – EnriqueBet May 27 '20 at 05:16