- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2023 01:34 AM - edited ‎05-28-2023 01:53 AM
I have a Custom scoped application where start and end date fields(Date and time field) were created, I was trying to calculate the duration between them and set it to Duration field. For some reason it is not working if start date and end dates are in different months. say like start date is 28th May and End date is 4th June it shows values as -51
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('x_1234_travel.DurCalculation');
ga.addParam('sysparm_name', 'calculateDays');
ga.addParam('sysparm_start', g_form.getValue('check_in_date'));
ga.addParam('sysparm_end', newValue);
ga.getXMLAnswer(setDiff);
function setDiff(response) {
alert(response);
g_form.setValue('duration_of_stay', response);
}
}
Script Include:
var DurCalculation = Class.create();
DurCalculation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
calculateDays: function() {
var startDay = this.getParameter('sysparm_start');
var endDay = this.getParameter('sysparm_end');
var start = new GlideDateTime(startDay);
var end = new GlideDateTime(endDay);
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
return days;
},
type: 'DurCalculation'
});
@Ankur Bawiskar @Alikutty A @sandeeprajput
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2023 09:55 AM
I have written the background script as per your requirement by changing the date format(YYYY-MM-DD). Please do the necessary changes in your script.
Please hit the thumbs up button and mark it as solution provided. Thanks a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2023 08:33 AM
HI @Resham Noor
I tried running the same code in my background-script it is working as expected. I would recommend check by putting alerts for g_form.getValue('check_in_date') & newValue in the client script. Maybe the date format is not correct I thinkso but check by putting alerts this would solve your issue.
If this issue solves please hit the thumbs up button and mark the solution provided. Thanks a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2023 08:36 AM
it's running correctly in my PDI but not in the client instance,
The value of check-in returned in the alert is of format - "28-05-2023 18:28:25" but in PDI it is "2023-05-28 18:28:25".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2023 09:55 AM
I have written the background script as per your requirement by changing the date format(YYYY-MM-DD). Please do the necessary changes in your script.
Please hit the thumbs up button and mark it as solution provided. Thanks a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2023 10:26 AM
It worked with the conversion as suggested by you, thanks for the quick help. FYI, tried formatting it by below but it did not work.
var date= new GlideDate();//replace new GlideDate() with the incoming date var date2 = date.getByFormat('yyyy/MM/dd');