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

Hi @Gillerla Rajesh,

 

Something is definitely off with as parseInt() should handle this for us. However, remember, in the world of JavaScript everything is a string unless manipulated correctly so you can often experience some weird behavior if all the steps aren't followed correctly.

 

To answer your question about removing the decimal, add this to the script:

var daysFixed = days.toFixed(0);

 

Remember to make sure you return daysFixed

 

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 working fine 

GillerlaRajesh_0-1718977525885.png

var daysFixed = days.toFixed(0); // may i know what this method will do

this method is in datetime API ?

 

can u explain me that what exactly this method will do internally.

Hi @Gillerla Rajesh,

 

toFixed(0) converts a number to a string, rounded to a specified number of decimals:

 

The parameter within the brackets will be used to round if required and greater than 0.

Examples below:

5.5 will be rounded to 6 if used as toFixed(0)

5.4 will be rounded to 5 if used as toFixed(0)

5.44 will be rounded to 5.4 if used as toFixed(1)

 

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

 

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Gillerla Rajesh 

 

You can try the below code :

 

On change client script : on end date:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var startdate=g_form.getValue('start_date');
   var enddate=g_form.getValue('end_date');
   var date=new GlideAjax('GetManagerDetailsScript');
   date.addParam('sysparm_name','getdatedifference1');
   date.addParam('sysparm_startdate',startdate);
   date.addParam('sysparm_end_date',enddate);
   date.getXMLAnswer(callback);

   function callback(response){
	var answer= response;
	alert(answer);
	g_form.setValue('get_number_of_days_from_start_date',answer);
   }

   //Type appropriate comment here, and begin script below
   
}

 

 

 

Script Include:

 

var GetManagerDetailsScript = Class.create();
GetManagerDetailsScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getdatedifference1: function() {
        var startvalue1 = this.getParameter('sysparm_startdate');
		var endvalue1=this.getParameter('sysparm_end_date');
        var sgd1 = new GlideDate();
        sgd1.setValue(startvalue1);
        var sgd2 = new GlideDate();
        sgd2.setValue(endvalue1);

        var duration = GlideDate.subtract(sgd1, sgd2);
        var total = duration.getDayPart();
        return total;

    },

});

 

Thanks and Regards

Sai Venkatesh