Calculate duration for "Days Open"

davekox
Kilo Explorer

Hello, I have created a duration field called "Days Open" which I would like to have reflect the total amount of time (in Days) elapsed between between the Opened date and Current date.

I know this has got to be very simple to do so I was looking at the "Duration" field to use as an example, but I haven't been able to find where the code is entered for the calculation. ??

Thanks in advance for any help you can provide. 🙂

7 REPLIES 7

geoffcox
Giga Guru

Here is a script include I have developed for duration calculations, called "CalculateDuration":



gs.include("PrototypeServer");

var CalculateDuration = Class.create();

CalculateDuration.prototype = {
calculate_duration : function(start,end) {
var duration = {};
duration.duration = gs.dateDiff(start,end,false);
duration.seconds = gs.dateDiff(start,end,true);
if (duration.seconds < 0) {
duration.seconds = 0;
}
duration.days = parseInt(duration.seconds * 100 / 86400,10)/100;
return duration;
}
};


This returns an object with three parameters:
obj.duration = a glide duration
obj.seconds = the difference in seconds between start and end.
obj.days = the difference in days between start and end.

It is important to know what time zone you are using for both start and end values so that you get the expected result.

So here is an example where I calculate the duration of a task:



var start = current.sys_created_on.getDisplayValue();
var end = current.work_end.getDisplayValue();
var td = new CalculateDuration();
var duration = td.calculate_duration(start,end);
current.u_task_duration = duration.duration;
current.u_task_duration_in_days = duration.days;


If this helps, please mark this question as "answered." Thanks!

Cheers,
Geoff.


davekox
Kilo Explorer

Thanks for the info, Geoff. I see where the script include is setup under System Definitions, but where does the calculation take place (or how is the script include called/triggered to calculate a duration)? If I go into Personalize Dictionary on the newly created field, I see a 'Calculated' check box and a 'Calculation' field, but it doesn't appear this is where the contents of your calculation example would go.

Again, thanks in advance for your help!


geoffcox
Giga Guru

The example above is in a business rule on the change_task table. The business rule setup to run before insert and update. (See attached image).


geoffcox
Giga Guru

Please remember to mark your question as answered. Thanks!