0

Editing note: The term java was quote enclosed as confusing JavaScript with Java is part of the OP problem. This happens to others too.

Im sort of new at "java", and I'm using it with Google Apps Script, although it seems that this question is "java" based. I would like to check 2 columns here, and for the second one if one date is equal to today or in the future have it return true. Not quite sure why this isn't working.

if(new Date(data[row][2]) <= new Date() && 
(new Date(data[row][3]) >= new Date() || new Date(data[row][3]) == new Date() ))
Rubén
  • 34,714
  • 9
  • 70
  • 166
tda
  • 241
  • 1
  • 4
  • 16

2 Answers2

0

Be careful comparing two objects with '==' as that will only compare the object references, not equivalency of the same object.

Try using the .before and .after commands with Date object, something like this:

Date today = new Date();
Date toCheck = new Date();

if (toCheck.after(today) || toCheck.equals(today)){

}
PoorBob
  • 413
  • 4
  • 16
  • Unfortunately because I'm using this in google apps script, the after and equals commands don't work. – tda Dec 10 '15 at 00:34
  • 1
    Does this help? http://stackoverflow.com/questions/11174385/compare-two-dates-google-apps-script – PoorBob Dec 10 '15 at 00:39
  • Hmm, sorry about that. Can you explain what you mean by "the after and equals command don't work?" What is the exact Date object you're using? Is it Java.util.Date? – PoorBob Dec 10 '15 at 01:02
  • Date objects don't work, so you have to save it as a variable instead – tda Dec 10 '15 at 01:04
  • Is this a javascript related question then? Did you mistake javascript for java? – PoorBob Dec 10 '15 at 01:06
  • Google Apps Script it's based on JavaScript not Java. The question was updated. – Rubén Oct 28 '16 at 14:11
  • So you downvote me because I answered the question as written? Almost a year after this dead question was asked? – PoorBob Oct 31 '16 at 21:41
0

When a new Date() is used it passes a date-time object to the variable (e.g. Fri Oct 28 21:16:39 GMT+05:30 2016). So the time part of the object fails the comparison logic even if there is difference by microsecond. This script loose the time part by using Utilities Class's formatDate method and compares Col A Date to Col B and returns whether its past,present or future.

function compareDate(){
  var ss = SpreadsheetApp.getActive();
  var sheet= ss.getActiveSheet();

  var Date1 = sheet.getRange("A1:A"+sheet.getDataRange().getLastRow()).getValues(); //Get ColA
  var Date2 = sheet.getRange("B1:B"+sheet.getDataRange().getLastRow()).getValues(); //Get ColB

  for(var i = 1; i<sheet.getDataRange().getLastRow();i++){  //iterate through date
  var fDate1 = Utilities.formatDate(Date1[i][0], "GMT","yyyy-MM-dd"); // formatting col A date without time
  var fDate2 = Utilities.formatDate(Date2[i][0], "GMT","yyyy-MM-dd"); // formatting col B date without time

     //Comparison logic
      if(fDate1 == fDate2){
          sheet.getRange("C"+i).setValue("Present");
      }
      else if (fDate1 < fDate2){
          sheet.getRange("C"+i).setValue("Future");
      }
      else if (fDate1 > fDate2){
         sheet.getRange("C"+i).setValue("Past");
     }
   }
}
Shrvan D
  • 69
  • 4