difference between calDateDiff and dateDiff

ankit110
Kilo Contributor

What is the difference between dateDiff and calDateDiff. From my understanding calDateDiff calculates the duration acording to default schedules. Can someone tell where can i find this schedule?

10 REPLIES 10

Do not use calDateDiff, it is deprecated. Use the GlideSchedule API. The following is an example from the API documentation about how to calculate a schedule based duration.



var startDate = new GlideDateTime('2014-10-16 02:00:00');


var endDate = new GlideDateTime('2014-10-18 04:00:00');


var schedule = new GlideSchedule();



schedule.load('090eecae0a0a0b260077e1dfa71da828'); // loads "8-5 weekdays excluding holidays" schedule


var duration = schedule.duration(startDate, endDate);


gs.info(duration.getDurationValue()); // gets the elapsed time in schedule


Hi Matthew,


I am looking for a way to use a schedule in a script that will return seconds.   I am going to put the script in a workflow timer activity.   I've discovered that I can't use the timer schedule fields when I am basing the timer on a script. (I was hoping I could use the schedule section along with the script, but it doesn't work that way)



This is the scenario:The seconds that is assigned to 'answer' in a timer activity comes from a custom property, but I want to adjust those seconds to accommodate a schedule.   The schedule is just business hours (M-F 8 - 5)



I can write a massive script to determine active and inactive seconds based on current date/time, but I was hoping there is some date functions that I can use.   There used to be all the functions- what parms they took and what was returned in the wiki, but now can't even access it...they are changed to go to the product documentation.   I knew I should have copied info so I could use later.   The documentation sheets don't provide too much help!



Do you have any thoughts on an easy script to return seconds that the timer activity should wait for based on when the activity starts?



Thanks,


Trena


We moved all the in-depth technical details of code implementation to the site: developer.servicenow.com


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDurationGetDurationValue



I think what you are looking for is to figure out the number of seconds it will be until a certain duration of in-schedule time has elapsed.


- Get the number of seconds to wait from a property (e.g. 144,000 seconds = 40 hours)


- Now spread those 40 hours across an 8-5 schedule to determine when the timer should end


- Now output the total difference in seconds between now and when the timer should end



I think you are going to want to use the GlideDateTime, GlideDuration and GlideSchedule API's. Something like this happens in the out-of-box PlannedTaskCalculator Script Include. Here's a very lightly tested variation of that code to suite your needs.



//this constructor only accepts a sys_id of a schedule - I know, I know, it is bad practice to hardcode sys_id's. Replace this sys_id with the one in your system.


var schedule = new GlideSchedule("04e664654a36232701a2247dcd8fc4cf");



//Here you replace my.seconds_to_wait with the name of your property


var durationMS = gs.getProperty("my.seconds_to_wait") * 1000;



//assuming we want to start the calculations from right now


var startDateGDT = new GlideDateTime();



//Create a duration record for the given number of milliseconds


var durationGD = new GlideDuration(durationMS);



//Add the duration into the schedule starting from right now.


var newEndDateGDT = schedule.add(startDateGDT, durationGD);



//Calculate the difference between now and the in-schedule end date.


var endDateMS = newEndDateGDT.getNumericValue();


var startDateMS = startDateGDT.getNumericValue();


var secondsDiffOutsideSchedule = (endDateMS - startDateMS) / 1000;



//output the results


gs.print(Math.round(secondsDiffOutsideSchedule) + " seconds");


gs.print(Math.round(secondsDiffOutsideSchedule/60/60/24) + " days");



You could adjust this code to your needs.


Thank you so much for that info!   I will work on getting something to work and report back!   Also...maybe the wiki redirect should go to the developer documentation instead of Product?   Maybe that is set to be a customer redirect instead of a developer redirect?


Thanks Mwatkins,


That was the solution I was looking for to make the timer dynamic by scripting and using a property and still be based on a schedule.   Worked great!



Thanks,


Trena