Ali Durrani
Ali Durrani

Reputation: 353

Comparing two dates(string) in JavaScript not working

I am trying to compare 2 dates in Google App Scripts using JavaScript. The problem i am having is the result is not right and i followed mulitple suggestions on Stackoverflow but it did not work.

This is my code:
- This is the code for getting a Date from today - 30days

  var date = new Date();
  var last_30 = date.setDate(date.getDate() - 30);
  var delete_date = Utilities.formatDate(date, 'Etc/GMT', 'dd/MM/yyyy');

This is my code:
- This is the code from getting Values from a spreadsheet that is in dateformat

    var row = values[i];  // Values from Spreadsheet cells    
    var dt = new Date(row[0]);
    var year = dt.getFullYear();
    var month = dt.getMonth() + 1 ;
    var day = dt.getDate();      
    var cell_date_value = ('0' + dt.getDate()).slice(-2) + '/'
             + ('0' + (dt.getMonth()+1)).slice(-2) + '/'
             + dt.getFullYear();

    Logger.log('cell:'+cell_date_value + ' delete:'+delete_date);

    if (Date.parse(cell_date_value) > Date.parse(delete_date)) {  
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++;
    }

This is my Logger.log():

[19-05-06 15:19:40:579 CEST] cell_value:03/05/2019  delete_before:06/04/2019
[19-05-06 15:19:40:580 CEST] cell_value:03/05/2019  delete_before:06/04/2019
[19-05-06 15:19:40:581 CEST] cell_value:03/05/2019  delete_before:06/04/2019
[19-05-06 15:19:40:581 CEST] cell_value:03/04/2019  delete_before:06/04/2019
[19-05-06 15:19:40:582 CEST] cell_value:03/03/2019  delete_before:06/04/2019
[19-05-06 15:19:40:583 CEST] cell_value:03/03/2019  delete_before:06/04/2019
[19-05-06 15:19:40:584 CEST] cell_value:03/02/2019  delete_before:06/04/2019

As you can see the cell_value is more than delete_before old on the 4 last rows but still this cell are not picked up under the if statement. The typeOf is string



I also tried this: (did not work):

var d1 = Date.parse(cell_value);
var d2 = Date.parse(delete_before);
if (d1 < d2) {
    alert ("Error!");
}

And this:

function isLater(str1, str2)
{
    return new Date(str1) > new Date(str2);
}

Anyone can see the problem?

Upvotes: 0

Views: 71

Answers (3)

Alexander_F
Alexander_F

Reputation: 2877

I think you can try to compare timestamps instead of time objects

var d1 = Date.parse(cell_value).getTime();
var d2 = Date.parse(delete_before).getTime();
if (d1 < d2) {
    alert ("Error!");
}

getTime() returns milliseconds spend sinse 1970, so you can compare 2 integers easy.

As example:

Date.parse(cell_value) != Date.parse(cell_value)

you are comparing 2 object instances. So basicly you are asking is this the same object. Not is this the same time.

Upvotes: 3

Ali Durrani
Ali Durrani

Reputation: 353

function date_splitter(date){
   var parts = date.split("/");
   return new Date(parts[2], parts[1] - 1, parts[0]);
}
if (date_splitter(cell_date_value) < date_splitter(delete_date)) {  
  sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
  rowsDeleted++;
} 

This worked!

Upvotes: -1

sultania23
sultania23

Reputation: 322

var date1 = new Date();
var date2 = new Date();
if(date1.getTime()>date.getTime())
{ 
  console.log("Date1 is greater")
}
else{
 console.log("Date2 is greater");

}

Date method "getTime()" gives time in milliseconds which can be used for comparison

Upvotes: 0

Related Questions