Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if one date is greater than the other using Google Script

I am trying to check if one date is greater than another date using google script. I am extracting a date from the spreadsheet and formatting it. Then I check it against today's date. If today's date is less than the extracted date, some functionality has to be done.This is as far as I have got.

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet1 = ss.getSheetByName("Sheet1"); //activating a particular sheet
var data = sheet1.getRange(2, 1, sheet1.getLastRow(), 100).getValues();

for (var row = 0; row < data.length; row++) {
    var todayDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy");
    var dueDate = new Date(data[row][17]);
    //Logger.log("Due: "+dueDate);
    var nRow = row+1;
    dueDate.setDate(dueDate.getDate()+1);
    var curDate=Utilities.formatDate(dueDate, "GMT", "MM/dd/yyyy");
    //Logger.log("Current: "+curDate);

    if(curDate>todayDate){
        Logger.log("Today: "+todayDate);
        Logger.log("Current: "+curDate);
        Logger.log("In");
    }
 }

I am able to log the values but the if() statement does not seem to work. Where have I gone wrong?

like image 645
Pallavi Prasad Avatar asked Aug 04 '16 11:08

Pallavi Prasad


Video Answer


1 Answers

Problem got solved. Solution was:

if(curDate.valueOf()>todayDate.valueOf()){}

It is now working.

like image 180
Pallavi Prasad Avatar answered Sep 30 '22 20:09

Pallavi Prasad