Based on two fileds i need to calculate the days.

Gillerla Rajesh
Tera Contributor

Hi there,

 

i have two fields start date time and end date time, the users will fill the form in portal they will select start date and end date then it will calulate difference between days. 

 

i done my code it's shoiwing days with decimal number, i don't need decimal number , how can i restrict

GillerlaRajesh_0-1718974707413.png

 

in syslog i am getting correct only but when i am setting the value it will taking decimal number 

GillerlaRajesh_1-1718974707483.png

 

 

Please find the source code for reference let me know i need to change anything in the script.

 

script include:

---------------------

dayCalculation: function() {

        var startDate = new GlideDateTime(this.getParameter('sysparm_sdate'));
        var endDate = new GlideDateTime(this.getParameter('sysparm_edate'));

        gs.log("start date is" + startDate);
        gs.log("end date is" + endDate);


        var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true);
        gs.log("different seconds "+diffSeconds);
        var day = diffSeconds / (60 * 60 * 24);
        var days=parseInt(day);
        //var days=Math.round(day);
        //var days=Number(day);


        gs.log("days is"+days);
        return days;
}
-----------
client side:
 
var sDate = g_form.getValue('u_start_time');
    var eDate = g_form.getValue('u_end_time');

    var gr = new GlideAjax('DayCalculation');
    gr.addParam('sysparm_name''dayCalculation');
    gr.addParam('sysparm_sdate', sDate);
    gr.addParam('sysparm_edate', eDate);

    gr.getXML(response);

    function response(res) {
        var result = res.responseXML.documentElement.getAttribute('answer');
        alert(result);
        g_form.setValue('u_days', result);
    }
 
 
any one suggest me how can i achive with integer number.
8 REPLIES 8

Robbie
Kilo Patron
Kilo Patron

Hi @Gillerla Rajesh,

 

Out of interest, what 'type' field is your 'u_days' field? Can you confirm by the dictionary please?

Also, can you confirm if this is a calalog/record producer variable or a standard table form that's rendered on the Portal. (It will help with setting the value as they slightly differ)

 

Your script when tested works fine for me and returns the value correctly as a String.

If this is not catalog/record producer variable, I suspect your 'u_days' field is of type 'decimal' - Please confirm.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.


Thanks, Robbie

Hi @Robbie

 

it's single line text field only, yes code is correct in backgroudn scipt it will returning the integer number, but when i am setting to the days field it will returning decimal number.

 

can u tell me how can i remove decimal number ?

is there havig any remove() is there ?

Hi @Gillerla Rajesh,

In your variable, go to the Type Specifications, and change the Validation Regex to Number.


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

Hi @Saloni Suthar , 

i already tried that one also , after setting the value it will showing error messge like it's not number like that.

GillerlaRajesh_0-1718977147280.png