dimanche 9 août 2020

If/else Javascript statement returns opposite value

I have a script that prints out two different responses if the current date is less than or greater than two dates (Expected Return Date) retrieved from a Google sheet. The two dates are the 2020-07-28 and 2020-08-28.

The code is below followed by the logs.

What I expect to happen is that when 2020-07-28 is retrieved, then the message "Expected Return Date is earlier than <todays date>" and when 2020-08-28 is retrieved, then the message "Expected Return Date is later than ".

The message "Expected Return Date is later than <todays date>" appears for both even though both dates are retrieved correctly.

Interestingly, when I change the comparison to "greater than" then the message "Expected Return Date is earlier than <todays date>" appears, which is wrong again.

function sendEmail()
{
    var now       = new Date().toLocaleDateString();
    var ss        = SpreadsheetApp.getActiveSpreadsheet();
    var startRow  = 1;
    var sheet     = ss.getSheetByName ('NC tracking')
    var numRows   = sheet.getLastRow()-1;
    var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
    var data      = dataRange.getValues();
    
    for (var i = 906; i <= 907; ++i) { 

        var row = data[i];
        ExpectedReturnDate = row[8].toLocaleDateString();
        console.log("The expected return date is " +ExpectedReturnDate);
        console.log("Todays date is " +now);
        if (ExpectedReturnDate < now) {                                      
            console.log("Expected Return Date is earlier than " +now);
        } else {
            console.log("Expected Return Date is later than " +now);
        } // end-if
    } // end-for
} // end-function

Log output below;

The expected return date is 28/07/2020
Todays date is 10/08/2020
Expected Return Date is later than 10/08/2020
The expected return date is 28/08/2020
Todays date is 10/08/2020
Expected Return Date is later than 10/08/2020

Aucun commentaire:

Enregistrer un commentaire