top of page

Experiencing challenges with ServiceNow support?

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

Mastering Time: Calculating the Difference Between Two Dates in ServiceNow (in Hours)

Updated: Mar 28

In the dynamic ServiceNow platform, accurately calculating the time elapsed between two events or dates is a common requirement. Whether you are tracking the duration of incidents, measuring task completion times, or setting up precise service level agreements (SLAs), understanding how to calculate the difference between two dates in hours is a fundamental skill for any ServiceNow user or developer. This article will guide you through the straightforward methods available in ServiceNow to achieve this, empowering you to effectively manage and analyze time-based data within the platform.


ServiceNow provides robust built-in functionalities to handle date and time calculations efficiently. When it comes to finding the difference between two dates and expressing that difference in hours, the combination of the GlideDateTime object and the gs.dateDiff() function offers a precise and reliable solution. Let's explore how to leverage these tools.

The Power of GlideDateTime and gs.dateDiff()

The GlideDateTime object in ServiceNow allows you to work with date and time values. You can create instances of this object from existing date/time fields on your ServiceNow records or by manually specifying date and time strings. Once you have your two dates represented as GlideDateTime objects, the gs.dateDiff() function comes into play.

The gs.dateDiff() function has the following syntax:

gs.dateDiff(start_datetime, end_datetime, return_in_seconds);

Here's what each parameter represents:

  • start_datetime: The starting date and time, usually a GlideDateTime object or a string representation that ServiceNow can understand.

  • end_datetime: The ending date and time, also typically a GlideDateTime object or a recognizable string.

  • return_in_seconds: A boolean value. Setting this to true will return the difference between the two dates in seconds as a string.


Step-by-Step Guide to Calculating the Difference in Hours

Let's walk through a practical example to illustrate how to calculate the difference between two dates in hours using these tools:

  1. Obtain Your Dates: First, you'll need to get the two dates you want to compare. These could come from fields on a ServiceNow record. For instance, if you want to find the duration between an incident's creation date and resolution date, you would retrieve the values of those fields. You can create GlideDateTime objects from these field values.

    JavaScript

var startDate = new GlideDateTime(current.sys_created_on); 
// Assuming 'current' is your record object var endDate = new GlideDateTime(current.resolved_at);

Alternatively, you can create GlideDateTime objects from specific date and time strings:

var startDate = new GlideDateTime("2024-03-20 09:00:00"); 
var endDate = new GlideDateTime("2024-03-21 17:30:00");
  1. Calculate the Difference in Seconds: Next, use the gs.dateDiff() function to find the difference between your two GlideDateTime objects in seconds.

var durationSeconds = gs.dateDiff(startDate, endDate, true); 
// Returns the difference in seconds as a string
  1. Convert Seconds to Hours: Now that you have the difference in seconds, you can easily convert it to hours by dividing by 3600 (since there are 3600 seconds in an hour).

var durationHours = durationSeconds / 3600;
  1. Optional: Round the Result: Depending on your needs, you might want to round the result to the nearest whole hour. You can use the Math.round() function for this.

var roundedDurationHours = Math.round(durationHours);

If you need more precision, you can skip this step or use other Math functions like Math.floor() or Math.ceil() as required.


Complete Code Example:

// Example with specific dates
var startDate = new GlideDateTime("2024-03-20 09:00:00");
var endDate = new GlideDateTime("2024-03-21 17:30:00");

var durationSeconds = gs.dateDiff(startDate, endDate, true);
var durationHours = durationSeconds / 3600;
var roundedDurationHours = Math.round(durationHours);

gs.print("The difference in hours is: " + roundedDurationHours); 
// Output: 33

Alternative Approach: Using GlideDuration

ServiceNow also offers the GlideDuration object, which represents a duration of time. You can obtain a GlideDuration object by subtracting one GlideDateTime object from another.

var startDate = new GlideDateTime("2024-03-20 09:00:00");
var endDate = new GlideDateTime("2024-03-21 17:30:00");

// Returns a GlideDuration object
var duration = GlideDateTime.subtract(startDate, endDate); 
gs.print(duration.getDisplayValue()); 
// Output: 1 Day 8 Hours 30 Minutes

While GlideDuration provides a human-readable representation of the time difference, it does not have a direct method to return the total difference solely in hours as a numerical value. You would need to parse the getDisplayValue() string or potentially use other methods of the GlideDuration object to extract the hour component and perform calculations if needed. For a direct calculation of the difference in hours as a number, the gs.dateDiff() method is generally more straightforward.


Practical Use Cases in ServiceNow

Calculating the difference between two dates in hours has numerous applications within ServiceNow:

  • Service Level Agreements (SLAs): Determining if a task or incident was resolved within the defined SLA timeframe, often measured in hours.

  • Task Management: Calculating the actual time spent on a task by comparing the start and end times.

  • Reporting and Analytics: Generating reports on resolution times, average task durations, or the time taken to complete specific processes.

  • Workflow Automation: Triggering actions in workflows based on the time elapsed between certain events.


Important Considerations

  • Ensure that the date and time values you are comparing are in a format that ServiceNow can correctly interpret.

  • Be mindful of time zones if your ServiceNow instance spans multiple geographical locations.

  • Decide whether you need the result to be a whole number of hours or if a decimal representation is more appropriate for your use case.


Conclusion

Calculating the difference between two dates in hours is a fundamental operation in ServiceNow that enables you to effectively track time-sensitive data. By utilizing the GlideDateTime object and the gs.dateDiff() function, you can easily and accurately determine the duration between two points in time in hours. Whether you are managing incidents, tracking tasks, or setting up SLAs, mastering this technique will significantly enhance your ability to work with time-based information within the ServiceNow platform. As a next step, try implementing these code examples in a ServiceNow script to see how they work with your own data and explore the various ways you can leverage this calculation in your workflows and reports.

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