top of page

Experiencing challenges with ServiceNow support?

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

How to Compare Two Dates (GlideDateTime) in ServiceNow Scripts

Updated: Mar 29

Comparing dates is a frequent requirement in ServiceNow development. Whether you're building business rules, scripting in workflows, or performing validations, knowing how to accurately compare GlideDateTime fields ensures that your logic executes reliably. Developers often encounter confusion about the correct way to compare date fields, especially when differentiating past, present, and future dates.

In this article, we explore how to effectively compare two GlideDateTime fields within a ServiceNow script, common pitfalls to avoid, and provide verified, practical solutions to simplify your scripting tasks.


Understanding the Issue: Why Date Comparisons Matter

Imagine you have a scenario where you need to check if the current date and time is later than the date an order was created. Your script retrieves dates using GlideDateTime, but how do you determine which date is greater?

Incorrectly comparing GlideDateTime values can lead to unexpected behavior, such as tasks triggering prematurely, notifications failing to send, or approval processes behaving unpredictably. Therefore, it’s crucial to clearly understand the correct methods to perform such comparisons.


Verified Methods to Compare GlideDateTime Fields

ServiceNow provides two main approaches to accurately compare GlideDateTime fields:


Method 1: Using getNumericValue()

The simplest and most effective method to compare dates in ServiceNow is by converting the GlideDateTime objects into numeric values. The method getNumericValue() returns the milliseconds since January 1, 1970, making it straightforward to perform comparisons using standard numeric operators.


Step-by-step Example:

Here's how you might practically implement this method in your script:

var gr = new GlideRecord('u_orders');
gr.query();

var orderDate;
var currentDate = new GlideDateTime(); // current system date/time

while (gr.next()) {
    orderDate = new GlideDateTime(gr.getValue('u_created')); 
    // date when order was created

    if (currentDate.getNumericValue() > orderDate.getNumericValue()) {
        gs.info("The current date is later than the order creation date.");
    } else {
        gs.info("The order creation date is today or in the future.");
    }
}

Practical Explanation:

  • currentDate.getNumericValue() and orderDate.getNumericValue() convert both dates into milliseconds.

  • A simple numeric comparison (>, <, =) provides a straightforward and accurate way to compare dates.


Method 2: Using GlideDateTime.subtract()

Another useful method involves calculating the time difference directly, using GlideDateTime.subtract(). This method returns a GlideDuration object, which can indicate whether one date is earlier or later than another and by how much.


Step-by-step Example:

Here’s how you might use subtract() to check the difference between two dates:

var gr = new GlideRecord('u_orders');
gr.query();

var orderDate;
var currentDate = new GlideDateTime();

while (gr.next()) {
    orderDate = new GlideDateTime(gr.getValue('u_created'));
    var duration = GlideDateTime.subtract(orderDate, currentDate);
    var daysDifference = duration.getDayPart();
    if (daysDifference >= 0) {
        gs.info("Order was created " + daysDifference + " days ago.");
    } else {
        gs.info("Order creation date is in the future.");
    }
}

Practical Explanation:

  • Using subtract() provides detailed differences, allowing you to calculate the exact number of days, hours, or minutes between two dates.

  • This method is particularly useful when you need not only a simple comparison but also information about the duration between the two dates.


Common Mistakes and How to Avoid Them


Mistake 1: Directly Comparing GlideDateTime Objects

Direct comparisons like this do not work:

if (currentDate > orderDate) {
    // Incorrect: may produce unreliable results
}

How to Avoid:

Always convert GlideDateTime objects to numeric values (getNumericValue()) or use subtract() to reliably compare them.


Mistake 2: Using Plain Strings or Non-GlideDateTime Objects

Avoid comparing date strings directly, as it can lead to inaccurate results due to timezone or formatting issues.


Alternative Solutions and Considerations

If you're working on client-side scripting (e.g., client scripts), comparing dates using JavaScript’s native Date object is another approach. However, for server-side scripting, using GlideDateTime remains the recommended method due to built-in timezone support and reliability.


Conclusion: Key Insights and Next Steps

Effectively comparing GlideDateTime fields in ServiceNow scripts ensures that business logic executes correctly and reliably. Remember these key points:

  • Use getNumericValue() for simple numeric comparisons of GlideDateTime objects.

  • Use GlideDateTime.subtract() if you need to know the duration or detailed difference between dates.

  • Avoid directly comparing GlideDateTime objects without conversion.


Recommended Next Steps:

  • Review your existing scripts for correct date comparisons.

  • Practice both methods (getNumericValue() and subtract()) to gain familiarity with their outputs.

  • Incorporate these practices into your standard scripting approach to enhance accuracy and performance.


By applying these techniques, you'll ensure your ServiceNow scripts are both robust and reliable when handling date comparisons.

Want to learn how to calculate the time difference between two dates? Read this article for the best practices in ServiceNow scripting.

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