Xpages Date Compare

Hello,

Looking for some advice about comparing dates in SSJS. I have tried the following code to see if the Due date is past todays date and if it is, to return “Overdue”.

I am using this in a viewPanel to colour the rows depending on the status :

var doc = varRowView.getDocument();

if( doc == null ){

return "";

}else{

var d = doc.getItemValueDate("BidDueDate");

var t = @Today();

if (d > t){

	return "Overdue";

}else{

	return "";

}

}

Never done this before in SSJS, I am getting the following error when the page loads :

com.ibm.xsp.exception.EvaluationExceptionEx: Error while executing JavaScript computed expression

Error while executing JavaScript computed expression

Script interpreter error, line=6, col=21: Error calling method ‘getItemValueDate(string)’ on an object of type ‘lotus.domino.local.Document [Static Java Interface Wrapper, lotus.domino.local.Document: lotus.domino.Document]’

The “BidDueDate” field is definitely a DateTime, so I dont understand the ‘String’ error

Any help really appreciated.

Subject: Use the right methods for the object types

I believe the issue is that the call to varRowView.getDocument() returns a NotesDocument object while the getItemValueDate method is only valid with a NotesXspDocument object. So you’ll need to use the getItemValue method (not getItemValueDate) on the NotesDocument to get the date value, which will be a NotesDateTime, and then you’ll need to convert that to a Java date object to use in your comparison routine.

Subject: Thanks - How to Convert?

Thanks very much,

Just one more question, could you give me an example of how to convert the Date?

i have tried loads of ways, but cant seem to get it.

Please do not laugh at the following code (I dont have a clue!) :

var doc = varRowView.getDocument();

if( doc == null ){

return “”;

}else{

var d = doc.getItemValue(“BidDueDate”);

var t = @Today();

if (d.toJavaDate().getDate() > t.toJavaDate().getDate()){

return “Overdue”;

}else{

return “”;

}

}

Any pointers would be much appreciated.

Subject: Example

First, the getItemValue method is going to return an array, so to get the first item in the array use the [0] notation.

I believe @Today (or any @Date formula) in SSJS returns a Java data, not a NotesDateTime, so you don’t need to convert it to a Java date. However, since your document date value is a NotesDateTime you can just do a NotesDateTime comparison - which I show in the below example.

Also I’d highly recommend you start specifying the data types when you declare your variables. That makes the type-ahead/auto-complete feature available for those variables and also makes it easier for both you and other developers following you to understand what type of data/object types are being worked with.

So what you are looking for is something like the following:

var doc:NotesDocument = varRowView.getDocument();

var d:NotesDateTime = doc.getItemValue(“BidDueDate”)[0];

var t:NotesDateTime = session.createDateTime(“Today”);

if (d.timeDifference(t) > 0 ) {

 return "Overdue";

}else{

 return "";

}

Subject: BRILLIANT!

Thank you very much - that works a treat!

Really appreciate your advice, will take it on board.

Thanks again.