Duration Calculator ServiceNow: How to Use It
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
The ServiceNow Duration Calculator is a built-in feature that calculates the time between two date-time values, typically in minutes or hours, and is used in workflows, business rules, and reports to measure how long a task, incident, or change took. You enter a start date/time and an end date/time, and the calculator returns the elapsed duration, which you can then format, store, or display. This article explains how the ServiceNow duration calculation works, how to use it in your instance, and common pitfalls to avoid.
Formula
In ServiceNow, the duration between two date-time values is simply the difference between them. The platform stores dates as integers (seconds since 1970-01-01), so subtracting the start from the end gives the elapsed time in seconds. To get minutes, divide by 60; to get hours, divide by 3600.
Formula: Duration (minutes) = (End Date/Time – Start Date/Time) / 60
Where:
- Start Date/Time – the earlier timestamp (e.g., when an incident was opened)
- End Date/Time – the later timestamp (e.g., when the incident was resolved)
- Duration – the elapsed time in minutes (or seconds, depending on the unit you choose)
In the calculator above, we use minutes after midnight as the input to keep it simple, but ServiceNow always works with full date-time values.
Worked Example
Using the default values from the calculator: start time = 480 minutes after midnight (8:00 AM) and end time = 1080 minutes after midnight (6:00 PM).
- Subtract start from end: 1080 – 480 = 600 minutes
- Convert to hours: 600 / 60 = 10 hours
The calculator shows a duration of 600 minutes, or 10.00 hours. In ServiceNow, if you had an incident opened at 8:00 AM and closed at 6:00 PM, the duration would be 10 hours (assuming no business hours or schedule rules are applied).
How ServiceNow Calculates Duration
ServiceNow provides several ways to calculate duration:
- Date/time subtraction – In a business rule or script, you can subtract two
glide_date_timeobjects or use thegetDuration()method. - Duration fields – Some tables have a
durationfield type that automatically stores the difference between two date/time fields (e.g.,sys_created_onandclosed_at). - Reports and analytics – You can create a report that shows the average or total duration for a set of records.
- Flow Designer – In a flow, you can use the “Calculate Duration� action to get the time between two dates, with options for business hours and time zones.
The most common method is to use a business rule that triggers when a record is updated, and then sets a duration field based on the difference between two date/time fields.
Using Business Rules for Duration
To calculate duration in a business rule, you typically do the following:
- Create a new field on your table (e.g.,
u_duration) with type “Duration�. - Write a business rule that runs on “Before� or “After� update, and set the duration field using
gs.dateDiff()or direct subtraction.
Example script:
var start = current.sys_created_on; var end = current.closed_at; var diff = gs.dateDiff(start, end, true); // returns seconds current.u_duration = diff / 60; // store in minutes
Note that gs.dateDiff() returns the difference in seconds, so you need to convert to the unit you want. Also, if you use a “Before� business rule, you must set the field before the record is saved.
Common Mistakes and How to Avoid Them
When working with duration in ServiceNow, users often make these errors:
- Forgetting time zones – ServiceNow stores dates in UTC, but displays them in the user’s time zone. Always convert to UTC before subtracting, or use the
glide_date_timemethods that handle time zones automatically. - Using business hours vs. calendar hours – If you want to exclude weekends and holidays, you need to use a schedule (e.g., “8-5 weekdays�) with the
schedule.duration()method. Otherwise, you get the raw elapsed time. - Rounding errors – Duration fields store in seconds, but you may want to round to minutes or hours. Use
Math.round()or the field’s display format. - Null values – If the end date is not set, the calculation will fail. Always check for null before subtracting.
Another common misconception is that the “Duration� field type automatically calculates itself. It does not – you must populate it via a business rule, flow, or script.
Duration with Schedules (Business Hours)
Often, you want the duration to count only business hours, not the total elapsed time. ServiceNow provides the schedule API to do this:
var schedule = new GlideSchedule('08:00:00', '17:00:00', 'America/New_York');
var duration = schedule.duration(start, end); // returns GlideDuration
This calculates the time between start and end, but only includes the hours defined in the schedule (e.g., 8 AM to 5 PM) and excludes weekends if you set the schedule to skip them. This is essential for SLA calculations, where you want to measure “business time� not just wall-clock time.
If you need to include holidays, create a schedule that lists the holidays as exceptions.
Comparing ServiceNow Duration with Other Tools
ServiceNow’s duration calculation is similar to what you’d find in Excel (subtract two dates) or in programming languages like JavaScript (using Date objects). The main difference is that ServiceNow has built-in schedule awareness and stores dates in UTC, which simplifies cross-time-zone calculations. However, it also means you need to be explicit about time zones and schedules, or you’ll get unexpected results.
If you’re used to Excel, note that ServiceNow does not have a simple DATEDIF function, but the gs.dateDiff() method is similar. For more complex calculations, you can use GlideDateTime’s getNumericValue() to get milliseconds.
Frequently Asked Questions
How do I get the duration in days in ServiceNow?
Divide the difference in seconds by 86400 (60*60*24). For example, var days = diff / 86400; where diff is the difference in seconds from gs.dateDiff().
Why is my duration negative?
If the start date is after the end date, the difference is negative. Check that your start and end fields are correctly populated, and consider using Math.abs() if you want the absolute value.
How do I calculate duration excluding weekends?
Use a schedule that excludes weekends (e.g., set the schedule to Monday-Friday only) and call schedule.duration(). This will give you the duration that falls within the schedule’s active time.
Can I use the Duration Calculator in Flow Designer?
Yes, Flow Designer has a “Calculate Duration� action that lets you pick two date/time values and a schedule (optional). It returns the duration in seconds, which you can then convert to minutes or hours.
What is the difference between duration and business duration?
Duration is the raw elapsed time (e.g., 48 hours for a weekend). Business duration is the time that falls within defined business hours (e.g., 16 hours if the weekend is excluded). ServiceNow uses schedules to calculate business duration.
Related
Formula used
in t | Start time (minutes after midnight) | min | 480 | 0 in t | End time (minutes after midnight) | min | 1080 | 0 out duration | Duration (minutes) | min | end - start | 0 out duration_hours | Duration (hours) | h | (end - start) / 60 | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.