- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 07:30 AM
Hi All,
could you please help me with the following requirement. we have 2 date/ time variables and a single line text variable. we want to calculate the date/ time difference from the two variables and paste the result in single line text filed.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 07:52 AM
it should be simple one.
write 2 onChange catalog client script i.e. 1 each on each date/time variable and use GlideAjax and get the difference and store in single line text variable
something like this
onChange client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var startDate = g_form.getValue('start_date_variable');
var endDate = g_form.getValue('end_date_variable');
if (startDate && endDate) {
var ga = new GlideAjax('DateDifferenceCalculator');
ga.addParam('sysparm_name', 'calculateDifference');
ga.addParam('startDate', startDate);
ga.addParam('endDate', endDate);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('single_line_text_variable', answer);
});
}
}
Script Include: It should be client callable
var DateDifferenceCalculator = Class.create();
DateDifferenceCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDifference: function(startDate, endDate) {
var start = new GlideDateTime(this.getParameter('sysparm_startDate'));
var end = new GlideDateTime(this.getParameter('sysparm_endDate'));
var duration = GlideDateTime.subtract(end, start);
return duration.getDisplayValue();
},
type: 'DateDifferenceCalculator'
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 07:38 AM - edited 12-18-2024 07:38 AM
Hi @Puneet4418 ,
check this thread, you can get your variable .
https://www.servicenow.com/community/developer-forum/how-to-get-the-time-difference-between-two-date...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 07:42 AM
Try something like this
var startDate = new GlideDateTime(your_variable);
var endDate = new GlideDateTime(your_second_variable);
var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); //returns value in seconds
your_string_variable.setDateNumericValue(diff*1000);
Please mark helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 07:52 AM
it should be simple one.
write 2 onChange catalog client script i.e. 1 each on each date/time variable and use GlideAjax and get the difference and store in single line text variable
something like this
onChange client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var startDate = g_form.getValue('start_date_variable');
var endDate = g_form.getValue('end_date_variable');
if (startDate && endDate) {
var ga = new GlideAjax('DateDifferenceCalculator');
ga.addParam('sysparm_name', 'calculateDifference');
ga.addParam('startDate', startDate);
ga.addParam('endDate', endDate);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('single_line_text_variable', answer);
});
}
}
Script Include: It should be client callable
var DateDifferenceCalculator = Class.create();
DateDifferenceCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDifference: function(startDate, endDate) {
var start = new GlideDateTime(this.getParameter('sysparm_startDate'));
var end = new GlideDateTime(this.getParameter('sysparm_endDate'));
var duration = GlideDateTime.subtract(end, start);
return duration.getDisplayValue();
},
type: 'DateDifferenceCalculator'
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 10:27 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader