1
votes

I'm having a weird issue comparing dates in my Google Apps Script.

For this, my sheet has a date in cell.getValue() so on the

e.range.setNote(cell.getValue() + " --- " + startDate);

line, the note shows two dates that appear identical, but they aren't because the following if statement is not satisfied. Am I missing something here with the comparisons? Thanks.

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sss = ss.getSheetByName("Sheet1");

  var row = e.range.getRow();
  var startDate = sss.getRange(row, 3).getValue(); //Wed Aug 10 2016 00:00:00 GMT-0600 (GALT)

  var cell = ss.getSheetByName("Sheet2").getRange(1, 1);
  e.range.setNote(cell.getValue() + " --- " + startDate); //Wed Aug 10 2016 00:00:00 GMT-0600 (GALT) --- Wed Aug 10 2016 00:00:00 GMT-0600 (GALT)

  if (cell.getValue() === startDate){
      cell.setBackground('blue');
    }  

}
1

1 Answers

4
votes

I think you need to compare as dates https://stackoverflow.com/a/493018/1393023

Replace

cell.getValue() === startDate

to

cell.getValue().getTime && startDate.getTime && cell.getValue().getTime() === startDate.getTime()