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

tanumoy
Tera Guru

Below thread will   be helpful:



Re: Business Duration vs. Duration


Ajai S Nair
Giga Guru

Hi Ankit,



calDateDiff(String, String, boolean)


Calculate the difference between two dates using the default calendar.



dateDiff(String, String, boolean)


Calculates the difference between two dates. This method expects the earlier date as the first parameter and the later date as the second parameter; otherwise, the method returns the difference as a negative value.



These kinks may help you:


GlideSystem Date and Time Functions - ServiceNow Wiki


Using Schedules - ServiceNow Wiki


Mwatkins
ServiceNow Employee
ServiceNow Employee

History


The Business Duration field was used as part of the old pre-2010 SLA engine (completely obsolete and not supported). Most customers no longer use this field.


The Duration field is also left over from earlier versions of the product and there are better ways of getting the information that it provides.



How they Work


There are a number of Business Rules that handle these operations for the various task tables. As of Calgary CCA6 here are the related Business Rules:


1) (incident) mark_resolved will set these fields when the incident_state goes to the resolved. https://<<instancename>>.service-now.com/nav_to.do?uri=sys_script.do?sys_id=d3b21f640a0a3c7400f6acab7de3f5f8


2) (incident) mark_closed will set these fields (if not already set by the the above rule) when the incident_state goes to closed. https://<<instancename>>.service-now.com/nav_to.do?uri=sys_script.do?sys_id=bf3f8917c0a8016400a867dc0794e8ad


3) (sc_task) Close Ticket. https://<<instancename>>.service-now.com/nav_to.do?uri=sys_script.do?sys_id=74d38bd0c611227d0151ca6b62ae87e6


4) (change_task) Close Ticket. https://<<instancename>>.service-now.com/nav_to.do?uri=sys_script.do?sys_id=9023ada50a0a0b01004228007704cf66


5) (problem) mark_closed. https://<<instancename>>.service-now.com/nav_to.do?uri=sys_script.do?sys_id=12a53c4fc6112275000bc7c04a87cfb6


6) (change_request) mark_closed. https://<<instancename>>.service-now.com/nav_to.do?uri=sys_script.do?sys_id=6e20e124c611228e00e44dd37ad1b842



calDateDiff


Durations are calculated based on the closed time when an incident is closed. Not when it is resolved. This is done by the "mark_closed" business rule on the incident table, for example. The rule will also use only 1 calendar and so the duration length will be based on that calendar where the 2 days of duration would represent 48 hours of calendar time.



In all cases (except "mark_closed" on change_request) these use the GlideSystem function calDateDiff



current.business_duration =gs.calDateDiff(current.opened_at.getDisplayValue(), current.closed_at.getDisplayValue(), false);



The calDateDiff function does not use a schedule but rather uses the calendar (System Policy -> Calendars). You only have 1 calendar which is "Monday thru Friday (9 - 5)".



If there were more than 1 calendar then it would just use the first one that it finds and so it is best to change the 1 calendar to what you want. This is because calDateDiff uses the default calendar - the first record in the sys_calendar table, arbitrarily returned by the database.


( See the WIKI http://wiki.servicenow.com/index.php?title=GlideSystem_Date_and_Time_Functions#calDateDiff.28String.... )



All of the above is not very flexible and that is why the new SLA engines use Schedules and also handle time zone issues better. With the old calendar, you really only have 1 timezone that must fit everyone.



NOTE: We suggest that you don't use calDateDiff function when tailoring your instance. It may work in our out-of-box scripts, but that is because they were created at a time when it was the recommended way to do this. calDateDiff is now legacy (i.e. deprecated).


I am looking to calculate a Change Requests After hours worked amount on Change Requests, I am looking for the total number of "After" Hours that occurred over the a given period of time.


I created 3 new duration fields, then 3 business rules to calculate their values:


Total actual hours - current.u_actual_hours_worked = gs.dateDiff(current.work_start,current.work_end,false);


Total business hours - current.u_business_hours_worked = gs.calDateDiff(current.work_start,current.work_end,false);


Total after hours -   current.u_after_hours_worked = (current.u_actual_hours_worked - current.u_business_hours_worked);



We have 1 calendar. Monday - Friday start 7:30 to 5:00.



Problem is occurring the Total business Hours business rule.   It's not calculating using the calDateDiff. Does it only work on the


Incidents when they are flagged as "Resolved"?   What about Change Orders?