Duration calculation between start and end date in a scoped application is not working

Resham Noor
Tera Contributor

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

ReshamNoor_0-1685262829216.png

 

 

 

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 

1 ACCEPTED SOLUTION

Adith Sanjay
Mega Sage

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.

image.png

Please hit the thumbs up button and mark it as solution provided. Thanks a lot!

View solution in original post

4 REPLIES 4

Adith Sanjay
Mega Sage

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!

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".

 

 

Adith Sanjay
Mega Sage

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.

image.png

Please hit the thumbs up button and mark it as solution provided. Thanks a lot!

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');