It's all about the answers!

Ask a question

Is there a way to calculate the number of business days between two dates - just exclude the weekends?


Abdelrahman Hassan (19116) | asked May 01 '18, 11:16 a.m.

Is there a way to calculate the number of business days between two dates excluding the weekends using a calculated-value script.

I can get the number of days but I don't know how to exclude the weekends.

----------------------------------

dojo.provide("com.rm.ProjectDaysValueProvider");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");

(function() {
    dojo.declare("com.rm.ProjectDaysValueProvider", null, {

        getValue: function(attribute, workItem, configuration) {
        
        // Get the start date and the end date and compute the difference in days.
        var startDate= dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.StartDate));
        var EndDate= dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.EndDaet));
//var startDate = workItem.getValue('StartDate');
//var endDate = workItem.getValue('EndDaet');
//var numOfProjectDays = " ";
if (startDate == null) {
Return " ";
}
else {
    if (EndDate == null) return " ";
                else return dojo.date.difference(startDate, EndDate, "day");
        }
    });
})();

Accepted answer


permanent link
Donald Nong (14.5k314) | answered May 01 '18, 9:06 p.m.

Basically you're asking the same question as this one.
https://jazz.net/forum/questions/248138/calculating-the-number-of-working-days-between-two-given-dates

You can try the "interval" argument in the dojo.date.difference() function, and choose the value "weekday".
https://dojotoolkit.org/reference-guide/1.9/dojo/date.html

Abdelrahman Hassan selected this answer as the correct answer

Comments
Abdelrahman Hassan commented May 28 '18, 10:50 p.m. | edited May 28 '18, 10:52 p.m.
Thank you Donald. Your answer was really useful. I was able to calculate the days difference between two dates excluding weekends.

I'm adding the script in a different answer.

One other answer



permanent link
Abdelrahman Hassan (19116) | answered May 28 '18, 10:52 p.m.
edited May 28 '18, 10:54 p.m.
Here is the script that worked for me after correcting some mistakes and following Donald's advise:

dojo.provide("com.example.DayDifference");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");

(function() {
    dojo.declare("com.example.DayDifference", null, {
    getValue: function(attributeId, workItem, configuration) {

try {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
var startDate= dojo.date.stamp.fromISOString(workItem.getValue("StartDate"));
var endDate= dojo.date.stamp.fromISOString(workItem.getValue("EndDaet"));
var nullDate = null;

if ( startDate != nullDate && endDate!= nullDate) {
var dateDifference = dojo.date.difference(startDate, endDate, "weekday");
return dateDifference.toString();
}

else {
return nullDate;
}

}

catch(err) {
            var txt;
            txt="There was an error on this page.\n\n";
            txt+="Error description: " + err.message + "\n\n";
            txt+="Click OK to continue.\n\n";
            alert(txt);
        }

    }
    });
})();

<link href="moz-&lt;a href=" extension:="" 5f1d3e77-ea94-4dd3-8aef-5b56d386bc16="" skin="" s3gt_tooltip_mini.css""="">extension://5f1d3e77-ea94-4dd3-8aef-5b56d386bc16/skin/s3gt_tooltip_mini.css" rel="stylesheet" type="text/css"> <style media="print" type="text/css"> #s3gt_translate_tooltip_mini { display: none !important; } </style>

Your answer


Register or to post your answer.