Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Script equal to date?

I'm creating a new spreadsheet that is set to give me a week ahead reminder for a review date. I'm trying to do a very basic test of checking if the date 7 days ahead from today is equal to the date that is on the spreadsheet.

When debugging this I noticed the times get added to the date so I set the times to be 00:00:00 to try and get a match, this still does not work.

function main(){
  var d = new Date();
  d.setDate(d.getDate()+7);
  d.setHours(0,0,0,0);
  for (var j = 53; j < lastrow-1; j++){
    var Staff = StaffObjects[j];
    var name = Staff.firstName + " " + Staff.surname;
    var onemonth = Staff.oneMonth;
    onemonth.setHours(0,0,0,0);
    if (d == onemonth){
      messagePT2+= name + "is due there one month review on " + onemonth + "<br>";
    }
  }
}

When debugging this, the values are exactly the same:

d = Fri Mar 07 2014 00:00:00 GMT-0000 (GMT) onemonth = Fri Mar 07 2014 00:00:00 GMT-0000 (GMT)

However this does not register as a match, why is this and how could i go about fixing this? Lastly. Some dates have different time zones such as this.. Wed May 07 2014 00:00:00 GMT+0100 (BST) is there any way to check for just the date?

Many thanks,

like image 308
Andy Avatar asked Oct 16 '25 14:10

Andy


2 Answers

Comparing date objects for equality does not work like that. It's better if you convert them to numbers (or strings) to compare. Try this:

if (d.getTime() == onemonth.getTime()){
  //etc
}

Note that when comparing with > and < and variants it does work (because it uses valueOf).

like image 150
Henrique G. Abreu Avatar answered Oct 18 '25 05:10

Henrique G. Abreu


In the comparison you should use .getTime() in each part because there are 2 different objects that will never be equal even if their value are equal.

So this condition should work :

if (d.getTime() == onemonth.getTime()){
like image 31
Serge insas Avatar answered Oct 18 '25 07:10

Serge insas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!