Date difference

sigmachiuta
Kilo Guru

I am trying to run a script when the time is within 1 minute of the due date time.   How can I make sure that I am comparing the correct values here?   I tried to run a background script that would show me the values to see why the script below is triggering almost immediately, but it returned null.

var gr= new GlideRecord('incident')

gr.addQuery('active', true);

gr.addQuery('number', 'INC00051753')

gr.query();

gs.print("date diff: " + gs.dateDiff(gr.due_date,gs.nowDateTime(), false));

var gr = new GlideRecord('incident');

gr.addQuery('active', true);

gr.addQuery('state', 'IN', '2,3,4' );

gr.query();

while(gr.next()){

var time = gs.dateDiff(gr.due_date.getDisplayValue(),gs.nowDateTime(),true);

time = parseFloat(time);

//set state

if( time <= 60) {

//set state

gr.state = 1;

gr.update();

}

}

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Right. dateDiff is a shortcut for a GlideDuration calculation:



GlideDuration gd = new GlideDuration(end.getNumericValue() - start.getNumericValue());



Depending on wether you want a numeric value or not, there is some conversion to a number then a string, but basically it's just "what is the amount of time I'd have to *add* to the start date to get to the end date". If your start date is in the past and your end date in the future, then you get a positive number. If you reverse them, you get a negative number (because you's have add a negative amount of time to the start date to get 'back' to the end date).



If you just want the *absolute value*, run the returned value through Math.abs():



var gr= new GlideRecord('incident');


gr.addQuery('active', true);


gr.addQuery('number', 'INC00051753');


gr.query();



if (gr.next())


      gs.print("date diff: " + Math.abs(gs.dateDiff(gr.getValue('due_date'), gs.nowNoTZ(), true)));


View solution in original post

11 REPLIES 11

Hi sigmachiuta,



What is your user account's timezone?


What is the system timezone?



Using Time Zones - ServiceNow Wiki



Use getValue() and gs.nowNoTZ() to get the value of the date/time field in UTC and the current date/time in UTC format, respectively.



var gr= new GlideRecord('incident');  


gr.addQuery('active', true);  


gr.addQuery('number', 'INC00051753');  


gr.query();  


 


if (gr.next())  


      gs.print("date diff: " + gs.dateDiff(gr.getValue('due_date'), gs.nowNoTZ(), false));



Do you now see the absolute difference between the dates as you expected to?


I didnt add .getDisplayValue to the gs.print so that is why it was showing different than expected. Also I switched the gs.dateDiff dates around and I am seeeing the expected results.



gs.dateDiff(gs.nowNoTZ(),gr.getValue('due_date'), true))


Great!


Can you mark your question as answered so other people who search for a similar question will know what to do?


Wait I dont get this.   I am testing with different dates and if I am on the same date/time for example when i change the day of the month and compare I get a negative number that doesnt seem right.



Different dates


*** Script: date diff: -86174   - ???- Difference


*** Script: 2015-08-20 15:38:30   - due date


*** Script: 2015-08-19 15:42:16   - current date/time



same date shows the current amount of seconds


*** Script: date diff: 8 sec. difference


*** Script: 2015-08-19 15:45:00   due date


*** Script: 2015-08-19 15:45:08 - current date/time


coryseering
ServiceNow Employee
ServiceNow Employee

Hi sigmachiuta,



The reason your test script returns null is you are not loading a result from the set.



var gr= new GlideRecord('incident');


gr.addQuery('active', true);


gr.addQuery('number', 'INC00051753');


gr.query();



if (gr.next())


      gs.print("date diff: " + gs.dateDiff(gr.getDisplayValue('due_date'), gs.nowDateTime(), false));



Once you have the test script returning results, you can figure out why the other script isn't doing what you want it to.