javascript - calculate days between 2 dates

118 views Asked by At

I have this problem : I am using google script with sheets and I can't find a way to get the number of days between due date (from the sheet) and current date. I want to send alert when current date is less then 30 days from due date.

maybe its related to the format of the date ? ( Sat Jun 13 2015 08:02:04 GMT+0300 (EEST))

please help,

function CheckedDatesToAlert()
{
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var sub = "Alert" ;
  var CurrentDate = new Date();
  var email = Session.getActiveUser().getEmail();


    for (var i = 1; i < data.length; i++) {
      var expireDate = data[i][6];

      if (CurrentDate.toDateString() < expireDate.toDateString()) {
          var subject = sub ;
1

There are 1 answers

0
CaptainNemo On

Try converting the expiry date to a date object, and then compare it to the original new date object, like so.

The greater than operator won't work if they are strings, but will if they are both date objects.

More info here

var expireDate = new Date(data[i][6]);