calculate difference between date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 04:03 AM
hello community,
i want to calculate the difference between start date and end date and populate the result on duration field.
i have create two scripts:
script include:
var lmsAjax = Class.create();
lmsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDateDiff: function() {
var d1 = new GlideDateTime();
d1.setDisplayValue(this.getParameter('sysparm_start'));
var d2 = new GlideDateTime();
d2.setDisplayValue(this.getParameter('sysparm_end'));
//var duration = new GlideDate().subtract(d1, d2);
var duration = new GlideDuration(d1.subtract(d2));
return duration.getNumericValue();
},
type: 'lmsAjax'
});
and client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 08:42 PM
Hello @mregragui_ext
you just update your script include function with the below script :
If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.
Thanks,
Vaibhav Nikam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 08:51 PM
Hi @mregragui_ext ,
You can use the below code to calculate the duration between two dates.
getTimeDiff();
function getTimeDiff(){
var startDate = current.u_start_date.getGlideObject();
var endDate = current.u_end_date.getGlideObject();
current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);
}
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Thanks & Regards,
Sumanth Meda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 09:18 PM
I think Instead of returning the numeric value of the duration directly, it should return a string representation of the GlideDuration object.
Script Include:
var lmsAjax = Class.create();
lmsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDateDiff: function() {
var d1 = new GlideDateTime();
d1.setDisplayValue(this.getParameter('sysparm_start'));
var d2 = new GlideDateTime();
d2.setDisplayValue(this.getParameter('sysparm_end'));
// Calculate the difference between the dates
var duration = new GlideDuration(d1.subtract(d2));
// Return the duration as a string representation
return duration.getDisplayValue();
},
type: 'lmsAjax'
});
Additionally, in the client script, you're passing the newValue of the 'end_date' field to the GlideAjax call, which seems incorrect. It should be the value of 'start_date' that needs to be passed as 'sysparm_start'
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var start = g_form.getValue('start_date');
var end = g_form.getValue('end_date');
var ga = new GlideAjax('lmsAjax');
ga.addParam('sysparm_name', 'getDateDiff');
ga.addParam('sysparm_start', start); // Pass the start date as parameter
ga.addParam('sysparm_end', end); // Pass the end date as parameter
ga.getXML(getDuration);
function getDuration(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('duration', answer); // Set the duration directly
}
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks