I have dates in the fifth column of my sheet.
function deleteOldData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();
var currentDate = new Date();
var daysago = new Date().setDate(currentDate.getDate() - 5);
daysago = new Date(Date.parse(daysago))
Logger.log(daysago)
for (i=lastrow;i>=2;i--) {
var tempdate = sheet.getRange(i, 5).getValue();
if(tempdate < daysago) {
sheet.deleteRows(2, i);
break;
}
}
}
I am using this code to delete the rows that have dates lesser than 5 days ago from today. It should delete only those rows that has dates less than 5 days ago from today but it is deleting all the rows expect the headers which i have omitted in the code.
Can you guys please help.
the date in my sheet looks like this:
7/1/2020 18:28:05