top of page

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

Counting the Days: Calculating Date Differences in ServiceNow

Updated: Mar 26


When working within the ServiceNow platform, you'll often encounter scenarios where you need to determine the duration between two dates. Whether it's tracking task completion times, measuring the lifespan of incidents, or calculating the time until a deadline, accurately figuring out the number of days between dates is a fundamental requirement. If you've ever struggled with this, you're in the right place. This article will walk you through a clear and effective method to calculate the difference in days using ServiceNow scripting.


Introduction: Why Understanding Date Intervals Matters in ServiceNow

The ability to calculate the interval between dates in ServiceNow is invaluable for various reasons. It allows you to:

  • Monitor Progress: See how long tasks have been open or in specific states.

  • Track Performance: Calculate resolution times for incidents or request fulfillment durations.

  • Automate Actions: Trigger events or notifications based on the age of a record.

  • Generate Insights: Report on trends and identify areas for improvement based on time-related data.

Mastering date difference calculations empowers you to build more sophisticated and insightful solutions within your ServiceNow environment.


Main Body: Leveraging GlideDateTime and its Power

ServiceNow provides a powerful tool for handling dates and times: the GlideDateTime object. This object offers various methods for manipulating and comparing dates. One of the most reliable ways to calculate the number of days between two dates involves converting them into a comparable numeric format – milliseconds – and then performing a simple calculation.

The Step-by-Step Approach

Let's say you have two dates you want to compare: an initial date (perhaps stored in a field called start_date) and an end date (which could be the current date or another date field).


Here’s how you can calculate the number of days between them:

  1. Retrieve Your Dates: First, you need to get the GlideDateTime objects representing your two dates. If one of your dates is a field value, you can retrieve it using getValue() and then create a GlideDateTime object from it. For the current date, you can instantiate a new GlideDateTime object.

// Assuming you are within a script where you have access to a GlideRecord object (e.g., 'current') 
var startDateValue = current.getValue('start_date'); // Get the value from the 'start_date' field 
var startDate = new GlideDateTime(startDateValue); // Create a GlideDateTime object 
var endDate = new GlideDateTime(); // Get the current date and time var currentDateOnly = endDate.getLocalDate();
// Get only the date part, without the time
  1. Convert to Milliseconds: The key to easily calculating the difference lies in converting both GlideDateTime objects to their numeric representation in milliseconds since the Unix epoch. You can achieve this using the getNumericValue() method.

var startTimeMs = startDate.getNumericValue();
var endTimeMs = currentDateOnly.getNumericValue();
  1. Calculate the Difference in Milliseconds: Now, subtract the earlier date's millisecond value from the later date's millisecond value.

var timeDifferenceMs = endTimeMs - startTimeMs;
  1. Convert to Days: To get the difference in days, you need to divide the millisecond difference by the number of milliseconds in a single day (which is 24 hours 60 minutes 60 seconds * 1000 milliseconds = 86,400,000 milliseconds). It's often useful to round down to the nearest whole number of days using Math.floor().

var daysDifference = Math.floor(timeDifferenceMs / (1000 * 60 * 60 * 24));
  1. Use Your Result: The daysDifference variable now holds the number of days between your two dates. You can then use this value for logging, calculations, or any other logic within your ServiceNow script.

gs.info('The difference in days is: ' + daysDifference);

Practical Example

Consider a scenario where you want to calculate how many days an incident has been open. You could use the sys_created_on field (the date the incident was created) as your start date and the current date as your end date. The script would look something like this:

var incidentGR = new GlideRecord('incident');
incidentGR.query();
while (incidentGR.next()) {
    var createdDateValue = incidentGR.getValue('sys_created_on');
    var createdDate = new GlideDateTime(createdDateValue);
    var currentDate = new GlideDateTime().getLocalDate();
    var createdTimeMs = createdDate.getNumericValue();
    var currentTimeMs = currentDate.getNumericValue();
    var timeDiffMs = currentTimeMs - createdTimeMs;
    var openDays = Math.floor(timeDiffMs / (1000 * 60 * 60 * 24));
    gs.info('Incident ' + incidentGR.number + ' has been open for ' + openDays + ' days.');
}

Important Considerations:

  • Time Zones: Be mindful of time zones when performing date calculations. GlideDateTime handles time zones based on the system settings. If you need to perform calculations in a specific time zone, you might need to use methods like setDisplayValue() with the desired time zone.

  • Date Only vs. Date and Time: The example uses getLocalDate() to get the current date without the time component. If you need to include the time in your calculation, you can directly use the GlideDateTime() constructor without calling getLocalDate().

  • Absolute Difference: If you only need the magnitude of the difference and don't care about which date is earlier, you can use Math.abs() on the timeDifferenceMs value.


Conclusion: Time is of the Essence in ServiceNow

Calculating the number of days between two dates in ServiceNow using the GlideDateTime object and its getNumericValue() method provides a precise and reliable way to measure time intervals. By following the steps outlined in this article, you can confidently incorporate date difference calculations into your ServiceNow scripts, enabling you to build more powerful and insightful applications and workflows. Start implementing this technique today to better understand and manage time-related data within your ServiceNow instance.

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page