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
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.