Google Apps Script: In spreadsheet, how to select cell with date format value in it and change it to that date plus 7 days (1 week)

17,978
function changeDate()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getRange("B1");
  var oldDate = cell.getValue().getTime(); //milliseconds from 1/1/1970
  var newDate = new Date();
  newDate.setTime(oldDate + (7*24*60*60*1000)); //add milliseconds in 7 days
  cell.setValue(newDate);  
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setTime

Share:
17,978
Kateb92
Author by

Kateb92

Updated on June 17, 2022

Comments

  • Kateb92
    Kateb92 almost 2 years

    I have a google spreadsheet and want to use Google Apps Script to activate cell B1 in the first sheet (currently formatted as a date - 1/27/2014) and change it to that date plus 7 days (2/3/2014). I am pretty new to Google Apps Script and have only successfully written a couple other functions. What I was thinking so far was:

    function changeDate() {
    
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheets()[0];
    
        var b1_date = sheet.getRange("B1").getValue();
        //...
    
    }