Based on two fileds i need to calculate the days.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2024 05:59 AM
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
in syslog i am getting correct only but when i am setting the value it will taking decimal number
Please find the source code for reference let me know i need to change anything in the script.
script include:
---------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2024 06:37 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2024 06:47 AM
Hi @Robbie ,
it's working fine
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2024 07:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2024 07:04 AM
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