Date/Time Script Include is returning value as NaN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2018 05:46 AM
Hi Team,
I have written a script include to find difference between date/time fields and system date to find difference in days. Our code is returning correct results when we directly use it in our schedule job.
I have written this code in a Script Include and calling this Script Include with Function name and passing Date/Time fields values to get difference but function is returning value as NaN.
"
var gr = new GlideRecord('cmdb_ci_computer');
//var timediff2 =0;
gr.setLimit(2);
gr.query();
while (gr.next()) {
var cmpyConfig = new CalculateDateDifference();
var timediff2 = cmpyConfig.Time_Difference(gr.sys_created_on);
gs.print("Return Value from Background Script Function Return "+timediff2);
}
"
Script Include Code : var CalculateDateDifference = Class.create();
CalculateDateDifference.prototype = Object.extendsObject(AbstractAjaxProcessor, {
initialize: function() {
},
Time_Difference:function(Date_Time_Value) {
var timdiff = 24*60*60;
//var datediff = 0;
var gtnow = gs.nowDateTime();
//var display = gr.sys_created_on.getDisplayValue();
var display = Date_Time_Value.getDisplayValue();
var gtreq = GlideDateTime(display);
var diff = gs.dateDiff(gtreq,gtnow,true);
var datediff = parseInt(diff/this.timdiff);
gs.log("Time Difference" +diff+ "Date/Time Passed passed "+Date_Time_Value);
return datediff;
},
type: 'CalculateDateDifference'
});
and Results :
*** Script: Time DifferenceNaNDate/Time Passed passed 2018-02-22 13:36:34
*** Script: Return Value from Background Script Function Return NaN
*** Script: Time DifferenceNaNDate/Time Passed passed 2018-02-22 13:36:37
*** Script: Return Value from Background Script Function Return NaN

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2018 04:04 AM
HI,
Instead of ParseInt Use Number and try. In your old code.
Instead of : var datediff = parseInt(diff/this.timdiff);
Use: var datediff = Number(diff/this.timdiff);
Thanks,
Ashutosh Munot