debugging calculated value javaScript
I have a calculated value script defined to calculate the number of days between two custom timestamp fields.
If the input form defines today's date as the start date and two days from today as the stop date, the result is 2 days as expected. If however I define the start date that is two days in the past, the result is still 2 days.
Is there something here that would prevent a date from being defined in the past? What am I missing?
Below is the code that I am using:
dojo.provide("calculateDuration");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
(function() {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
dojo.declare("calculateDuration", null, {
getValue: function(attribute,workItem, configuration) {
var startDate = workItem.getValue("startDate");
var stopDate = workItem.getValue("stopDate");
var startMS = startDate.getTime();
var stopMS = stopDate.getTime();
var differenceMS = Math.abs(stopMS - startMS);
var msInADay = 1000 * 60 * 60 * 24;
return Math.floor(differenceMS / msInADay);
}
});
})();
Accepted answer
Read:
-
https://jazz.net/library/article/1003
- https://jazz.net/wiki/bin/view/Main/AttributeCustomization
- Lab 5
- https://jazz.net/library/article/1360
This is an example for how to compare dates:
/*********** * Licensed Materials - Property of IBM * (c) Copyright IBM Corporation 2011. All Rights Reserved. * * Note to U.S. Government Users Restricted Rights:
* Use, duplication or disclosure restricted by GSA ADP Schedule * Contract with IBM Corp. ***********/ dojo.provide("org.example.DateValidator_30_days_later");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 Severity = com.ibm.team.workitem.api.common.Severity; var Status = com.ibm.team.workitem.api.common.Status;
var DateValidator = dojo.declare("org.example.DateValidator_30_days_later", null, {
validate: function(attributeId, workItem, configuration) { var severity= "ERROR"; var message= "Date must be at least 30 days from now"; // Work Item new: not initialized - provides todays date. Once created provides the creation date var beginDate = dojo.date.stamp.fromISOString(workItem.getValue("com.ibm.team.workitem.attribute.creationdate")); // Get the current attribute's value and make a Date object from it console.log("I got: ["+workItem.getValue(attributeId)+"]"); var endDate= dojo.date.stamp.fromISOString(workItem.getValue(attributeId)); // Compare the two dates returns negative value due to order var compare = -(dojo.date.difference(endDate, beginDate, "day")); if (compare>= 30) { // make sure endDate is not earlier than beginDate + 30 return Status.OK_STATUS; } else { return new Status(Severity[severity], message + " current value: [" +endDate + "] and difference is " + compare); } }
}); })();
2 other answers
You should Gooogle for JavaScript Funktion to calculate with dates and read the documentation about attribute customization in the team wiki. There is also a process enactment workshop in the library you can download and read to get at this information. You have to do a conversion to correctly read and write dates which is described in the attribute customization API and plus and minus is not needed to calculate date differences.
I can see nothing wrong in your code. All you are working on are just numbers, and there is nothing built-in to differentiate between any data and "now". If you are saying you get 2 instead 4, I suspect there is something wrong outside of the code (well, if you simply retrieve the data from a wrong attribute, only you can tell). Adding console.log() in your code will make it easy for you to see.
https://www.w3schools.com/js/js_debugging.asp