Issues with Duration calculation in scoped app
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2017 08:50 AM
Hi Community,
I'm trying calculate the duration between two dates, but separate them out by Year, Month, and Date. For example, if Start Date = Jan 1, 2016 and End Date = July 4, 2017, I want my Year field to show 1 year, my Month field to show 7 months, and my Day field to show 4 days. Here's what my form looks like:
I've seen other posts about duration and can't seem to get it to work in our scoped application. Here's what I've done with the script include:
Name: counttime
Client callable: true
Accessible from: All Application Scopes
var counttime = Class.create();
counttime.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
calcutime: function() {
var start= this.getParameter('sysparm_strt');
var end = this.getParameter('sysparm_end');
var answer = gs.dateDiff(start, end, true);
answer = answer/(60*60*24*365);
return answer;
},
type: 'counttime'
});
And my client script on my table looks like this:
Name: Duration
Type: onChange
Field: Year
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var strt = g_form.getValue('from');
var end = g_form.getValue('to');
var ajax = new GlideAjax('counttime');
ajax.addParam('sysparm_name','calcutime');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXMLWait();
g_form.setValue('year', answer);
}
When I enter in my From and To dates, no numbers appear in my Year field. Any advice on how to fix this?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2017 12:01 PM
Hi,
dateDiff() method is not allowed in scoped applications. Instead you can use:
subtract(GlideDateTime start, GlideDateTime end)
Sample code:
var gdt1 = new GlideDateTime("2011-08-28 09:00:00");
var gdt2 = new GlideDateTime("2011-08-31 08:00:00");
var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
gs.info(dur.getDisplayValue());
If you want numeric value of the duration which is in milliseconds:
gs.info(dur..getNumericValue());
If the field is Date rather than Date Time, use:
var dur = GlideDate.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
Sample Code:
var sgd1 = new GlideDate();
sgd1.setDisplayValue('2014-07-18');
var sgd2 = new GlideDate();
sgd2.setDisplayValue('2014-07-19');
duration= GlideDate.subtract(sgd1, sgd2);
gs.info(duration.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2017 10:14 PM
Thanks gowrisankar! I tried the subtract method, but it still doesn't work, any other ideas?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2017 10:38 PM
Please check if concept given here is helpful: Get year, month and day from date difference : Date Calculation « Date « JavaScript Tutorial

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2017 01:16 AM
Hi David,
Can I know what value are getting using Subtract method?
You have to do this calculation:
parseInt(duration.getDisplayValue()/1000) = seconds
parseInt(duration.getDisplayValue()/1000*60) = minutes
parseInt(duration.getDisplayValue()/100*60*24 )= hours
parseInt(duration.getDisplayValue()/100*60*24*365) = years