It's all about the answers!

Ask a question

How to compare two Date attributes in a script-based validator?


Arjun Pande (3212) | asked Apr 13 '16, 2:06 a.m.
converted to question Apr 14 '16, 1:04 a.m. by Donald Nong (14.4k213)

Hi Ralph,

I need to write a dojo script where I can compare 2 dates, one is IBM out of the box field due date and other is system current date. I am writing a validator script where users will not be allowed to enter the Due date < Current Date. Due Date can be equal to or greater than current date. Below is the dojo script for the same.

dojo.provide("DueDateValidation");

 dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); // To access the work item attributes
 dojo.require("com.ibm.team.workitem.api.common.Status"); // To create a status object to return
 dojo.require("com.ibm.team.workitem.api.common.Severity"); // To create a severity for the status
 dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
 dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings


 (function() {
 
 var Status = com.ibm.team.workitem.api.common.Status;
 var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;

 var DateValidator = dojo.declare("DueDateValidation", null, {

     validate: function(attributeId, workItem, configuration) {
         var severity = "ERROR";
         var message = "Due Date cannot be less then Current Date";
     var GetCurrentDate = new Date();
         var CurrentDate = GetCurrentDate.getTime().toString();
     var GetDueDate = dojo.date.stamp.fromISOString(workItem.getValue(com.ibm.team.workitem.api.common.WorkItemAttributes.DUE_DATE));
     var duedate = GetDueDate.getTime().toString();
     var PHED = dojo.date.stamp.fromISOString(workItem.getValue("HandoverPlannedEndDate"));
     var StringPHED = PHED.getTime().toString();

             
         // Compare the two dates and make sure DueDate is not earlier than CurrentDate
         if ((workItem.getValue(WorkItemAttributes.TYPE)=="change_request") && (duedate >= CurrentDate)) {
             return Status.OK_STATUS;
         }else {
             return new Status(Severity[severity], message);
         }
     }
 });
 })();

I am using formal process management template and the CLM version is 6.0. Please let me know your thoughts on this. Any help is appreciated. Thanks.


Comments
Donald Nong commented Apr 14 '16, 1:05 a.m.

Posting a question as an answer to in an old post does not get you anywhere. I've converted it to a question now.


Donald Nong commented Apr 14 '16, 1:16 a.m.

Your script is quite messy, to say the least. Why do you convert a String to a Date, and then to a String, and finally compare two String values? What is the purpose of the variables PHED and StringPHED? You did not say whether the script works for you or not - have you tried it?


Ralph Schoon commented Apr 14 '16, 2:33 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Posting on an old question is not a good idea and trying to direct questions in specific users directions neither.

3 answers



permanent link
Arjun Pande (3212) | answered Apr 14 '16, 1:35 a.m.
The template I am using is Formal Process Management Template and the RTC version is 6.0. So the id's of the due date comes from that template.

permanent link
Ralph Schoon (56.2k23642) | answered Apr 14 '16, 2:08 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
See https://jazz.net/forum/questions/220004/how-the-below-code-works-for-date-validation/220007

permanent link
Ralph Schoon (56.2k23642) | answered Apr 14 '16, 2:31 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
I had to ban one answer as that had only philosophical content and a link to a resort page.

Your answer


Register or to post your answer.