My delay script is not working
![](http://jazz.net/_images/myphoto/7e5088aa0244588a8e745c289c57a9ba.jpg)
Here's my script:
I have a custom attribute Baseline End Date (ID: project.baselineduedate | Type: Timestamp) and I want to calculate the difference between the current date and the Baseline End Date and show it.dojo.provide("example.calculated.TotalDelay");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date.stamp");
(function() {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
var fromISOString = dojo.date.stamp.fromISOString;
dojo.declare("example.calculated.TotalDelay", null, {
getValue: function(attribute, workItem, configuration) {
var BaselineEndDate = fromISOString(workItem.getValue(project.baselineduedate));
if (BaselineEndDate) {
var DelayInSeconds = (Date.now() - BaselineEndDate.getTime()) / 1000;
var days = parseInt(DelayInSeconds / 86400);
return days + " d ";
}
return "";
}
});
})();
I created the custom attribute "Project Delay" of type Decimal, calculated value: Project Delay, dependencies Baseline End Date and Read-Only presentation. I still don't find my error.
Any help is hugely appreciated.
Georges
Accepted answer
![](http://jazz.net/_images/myphoto/7e5088aa0244588a8e745c289c57a9ba.jpg)
You can not simply use + and - to operate on dates. You can not simply return a data. See https://jazz.net/wiki/bin/view/Main/AttributeCustomization#API_for_Javascript and look at the Timestamp section. Search the internet for how to operate on dates with JavaScript
Here is example code for calculating:
Here is example code for calculating:
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); // To access the work item attributes 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 . . . . . 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
One other answer
![](http://jazz.net/_images/myphoto/7e5088aa0244588a8e745c289c57a9ba.jpg)
Solved
dojo.provide("org.example.workitems.providers.delaysindays");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");
(function() {
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
dojo.declare("org.example.workitems.providers.delaysindays", null, {
getValue: function(attributeId, workItem, configuration) {
// Get the creation date and the current date and compute the difference in days.
var baselineDueDate= dojo.date.stamp.fromISOString(workItem.getValue('project.baselineduedate'));
var currentDate= new Date();
var dayDiff= -dojo.date.difference(currentDate, baselineDueDate, "day");
return dayDiff + " days";
}
});
})();