Get today date in google appScript

后端 未结 5 1284
旧时难觅i
旧时难觅i 2021-02-01 15:48

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 = Spreadsheet         


        
相关标签:
5条回答
  • 2021-02-01 16:08
    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
    }
    
    0 讨论(0)
  • 2021-02-01 16:13

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

    0 讨论(0)
  • 2021-02-01 16:13

    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);
    }
    
    0 讨论(0)
  • 2021-02-01 16:20
    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.
    }
    
    0 讨论(0)
  • 2021-02-01 16:28

    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); 
    }
    
    0 讨论(0)
提交回复
热议问题