简体   繁体   中英

Google apps script if statement goes wrong

So, I am currently trying to check if a date from my Google spreadsheet cell matches some date I got from a HTML document. Here is the function that I use:

 for (var i=0; i < data.length; i++) {
   var testDate =  subDaysFromDate(data[i][0],1)
   var DateToCheck = Utilities.formatDate(testDate, "GMT", "dd-MM-yyyy");
   var DateToCheckBefore = DateToCheck.split("-");
   var DateToCheckAfter = new Date(DateToCheckBefore[2], DateToCheckBefore[1]-1, DateToCheckBefore[0]); //dit is de datum van een row

   var d1 = $d1.split("-");
   var d2 = $d2.split("-");

   var from = new Date(d1[0], d1[1]-1, d1[2]);  // -1 because months are from 0 to 11
   var to   = new Date(d2[0], d2[1]-1, d2[2]);

    if(DateToCheckAfter >= from && DateToCheckAfter <= to){
      var dataCheck = sheet.getRange("T2").getValue();

      if(DateToCheckAfter == dataCheck){
       Browser.msgBox('9 march?!', Browser.Buttons.OK);
      }

      Logger.log(dataCheck);
      Logger.log(DateToCheckAfter);
    }

  }

As you can see it is obviously not finished yet, my question is; why does the following if statement not work??

if(DateToCheckAfter == dataCheck)
{
  Browser.msgBox('9 march?!', Browser.Buttons.OK);
}

When I log the variables, the logger says:

[15-03-18 14:06:33:227 CET] Mon Mar 09 00:00:00 GMT+01:00 2015
[15-03-18 14:06:33:228 CET] Mon Mar 09 00:00:00 GMT+01:00 2015

So why is my if statement not working?

Well the answer was pretty simple..

DateToCheckAfter was a Date object and dataCheck was a string

if(DateToCheckAfter.toString() == dataCheck)
{
  Browser.msgBox('9 march?!', Browser.Buttons.OK);
}

Fixed it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM