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-13-2018 06:06 AM
HI,
We dont have to use var display = Date_Time_Value.getDisplayValue();
Use: var display = Date_Time_Value; //BEcoz this is glidedatetime object and gs.nowDateTime() as well.
So this will work for you.
Thanks,
Ashutosh Munot
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2018 06:45 AM
Hi Ashutosh,
thanks for your reply.
I modify code as per your comments but still script include is returning NaN.
Below are Code in Schedule Job :
"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.getDisplayValue());
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 display = Date_Time_Value;
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'
});
"
Output :
*** Script: Time Difference72841Date/Time Passed passed 2018-03-12 10:26:35
*** Script: Return Value from Background Script Function Return NaN
*** Script: Time Difference73070Date/Time Passed passed 2018-03-12 10:22:46
*** Script: Return Value from Background Script Function Return NaN
Your valuable comments will be appreciated.
thanks,
Basant Soni

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2018 07:16 AM
HI,
Request you to use below code:
Instead of this : var datediff = parseInt(diff/this.timdiff);
Use: var timediff = this._calcDateDiff(diffTYPE, diff); // _calcDateDiff is a function
//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
}
See below is your full code:
Time_Difference:function(Date_Time_Value) {
var timdiff = 24*60*60;
var diffTYPE = 'day';
var gtnow = gs.nowDateTime();
//var display = gr.sys_created_on.getDisplayValue();
//var display = Date_Time_Value.getDisplayValue();
var display = Date_Time_Value;
var gtreq = GlideDateTime(display);
var diff = gs.dateDiff(gtreq,gtnow,true);
var timediff = this._calcDateDiff(diffTYPE, diff);
gs.log("Time Difference" +diff+ "Date/Time Passed passed "+Date_Time_Value);
return timediff ;
},
//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
},
type: 'CalculateDateDifference'
});
Thanks,
Ashutosh Munot
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2018 03:59 AM
Hi Ashutosh,
thx again for your help, I have changed my code in script include and tried it. below are updates .
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 diffTYPE = 'day';
var gtnow = gs.nowDateTime();
//var display = gr.sys_created_on.getDisplayValue();
//var display = Date_Time_Value.getDisplayValue();
var display = Date_Time_Value;
var gtreq = GlideDateTime(display);
var diff = gs.dateDiff(gtreq,gtnow,true);
var timediff = this._calcDateDiff(diffTYPE, diff);
gs.log("Time Difference" +diff+ "Date/Time Passed passed "+Date_Time_Value);
return timediff ;
},
//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
},
type: 'CalculateDateDifference'
});
My Code :
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.getDisplayValue());
gs.print("Return Value from Background Script Function Return "+timediff2);
}
Results :
*** Script: Time Difference79155Date/Time Passed passed 2018-03-12 10:26:35
*** Script: Return Value from Background Script Function Return 0.9161458333333333
*** Script: Time Difference79384Date/Time Passed passed 2018-03-12 10:22:46
*** Script: Return Value from Background Script Function Return 0.9187962962962963
Since i have to compare days and values returning in fractions that's why i was putting Parse Int.
thx,
Basant