0

For some reason my code is returning the backwards answer in my date comparison. (SSJS)

I have 2 code blocks, one is below, the other simply changes the line if(dtCreated < dtCutoff) to if(dtCreated > dtCutoff)

try{

var sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
var dtCreated = document1.getItemValueDate("CreatedDate");
var dtCutoff = new Date(2002, 03, 22, 00, 30);

dtCreated = dtCreated == null?"":sdf.format(dtCreated);
dtCutoff = dtCutoff == null?"":sdf.format(dtCutoff);
print("ONE: Created: " + dtCreated);
print("ONE: Cutoff: " + dtCutoff);

if(dtCreated < dtCutoff) {
print ("1.1  created before cutoff return true");
    return true;
}else{
print ("1.2  created before cutoff return false")
    return false;
}

}catch(e){
    openLogBean.addError(e,this.getParent());
}

For some reason, it seems to be getting the result mixed up, where by the created date is after the cutoff and date yet it says created date is before the cutoff date, and vice versa.

Any ideas why? Date stuff has always been my achilles heel. Each code block is used in the loaded property of a custom control. My end goal is to show 1 custom control or the other if a document was created before or after a certain date.

Print from the console is below, thanks:

HTTP JVM: ONE: Created: 26-02-2020
HTTP JVM: ONE: Cutoff: 22-04-2002
HTTP JVM: 1.2  created before cutoff return false
HTTP JVM: TWO: Created: 26-02-2020
HTTP JVM: TWO: Cutoff: 22-04-2002
HTTP JVM: 2.1  created after cutoff return true
Chris Richards
  • 695
  • 5
  • 14

1 Answers1

3

The problem is that you're comparing text strings, not dates. As such "22...." is earlier alphabetically than "26....". To compare, you either want to get the field value as a Java date and use .before(). This answer covers getting a Java date from a field Set a Java date Object from a Notes DateTime Object. Alternatively, create a Domino DateTime for dtCutOff and use the Domino DateTime's timeDifferenceDouble() method.

Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
  • Thanks for this Paul, will take a look, I thought that by creating 2 date objects using new Date(); meant that I could then simply use the comparison operators to do my comparison as they would be treated as date objects, but I guess not! – Chris Richards Apr 29 '20 at 11:36
  • When I try to import lotus.domino.DateTime; in my SSJS code window I get the error: " encountered "" at line 1, column 13, was expecting one of: " Do I need to reference a package from the package explorer view by clicking JAR file and use "Build Path/Add to Build Path? If so which JAR? Or am I completely wrong? Complete novice when it comes to JAVA unfortunately. Thanks – Chris Richards Apr 29 '20 at 12:34
  • `import....` is Java syntax. For SSJS you need to cast a variable to the relevant class name and the class names are designed to be more similar to LotusScript. So: var dtCreated:lotus.domino.NotesDateTime = document1.getItemValueDate("CreatedDate"); var dtCreatedJava:java.util.Date = dtCreated.toJavaDate(); There may be minor errors here, it's a long time since I coded in SSJS and I don't have Domino Designer to hand. The question linked gives the method call to get a Java date from a Domino DateTime though. – Paul Stephen Withers Apr 29 '20 at 12:54
  • Makes much more sense, thanks! Seems to failing when I turn it into a javadate though. dt.toJavaDate(); it prints N1 but never prints N2. I've double checked at the syntax is correct. var dt:NotesDateTime = document1.getItemValueDate("CreatedDate"); print("N1"); var jdt:Date = dt.toJavaDate(); print("N2"); Think I'll just try out the timeDifferenceDouble() method of NotesDateTime – Chris Richards Apr 29 '20 at 13:35
  • Now it fails before printing "5". I honestly don't know why I struggle so much with date comparison?!?! print ("1"); var dtCutoff = new Date(2002, 03, 22, 00, 30); print ("2 Cutoff: " + dtCutoff); var dtCutoff2:NotesDateTime = dtCutoff; print ("3 Cutoff 2: " + dtCutoff2); var dtCreated:NotesDateTime = document1.getItemValueDate("CreatedDate"); print ("4 Created: " + dtCreated); var days:int = dtCutoff2.timeDifferenceDouble(dtCreated) / 86400; // 86400 seconds in a day print ("5"); print("This document was created " + Math.floor(days) + " days ago."); – Chris Richards Apr 29 '20 at 13:57
  • It mail fail casting to an int. Best option is error handling to get a more comprehensive cause, something like XPages OpenLog Logger (disclaimer, I wrote it) – Paul Stephen Withers Apr 29 '20 at 16:07
  • Don't worry Paul, I make ample use out of your openLogger! The error is: Script interpreter error, line=11, col=22: [TypeError] Error calling method 'timeDifferenceDouble(Date)' on an object of type 'Date [JavaScript Object]'. The requirement for this code has actually gone now, however it would be good to understand why my code isn't working so that I can learn from it – Chris Richards Apr 29 '20 at 16:13
  • Both need to be the same data type. Date (java.util.Date) and DateTime (lotus.domino.DateTime) are different. The key part is to make sure you create the same data type. java.util.Date has a before() method which is more intuitive. lotus.domino.DateTime requires use of timeDifferenceDouble() to compare. There is a timeDifference() method, but when comparing dates with large differences, that can cause an overflow error (number larger than int), so I always use timeDifferenceDouble(). – Paul Stephen Withers Apr 30 '20 at 07:26