top of page

Experiencing challenges with ServiceNow support?

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

How to Get and Use the Current Date in ServiceNow Client Scripts

Updated: Mar 30

In ServiceNow, client scripts are vital tools used to enhance user experience by providing real-time validation and immediate feedback. A common requirement developers face is validating date fields against the current date. For instance, ensuring an end date selected by a user does not exceed a certain period, such as one year from today's date. This article explains how to effectively get and utilize the current date in a ServiceNow client script, providing step-by-step guidance and practical examples to resolve common issues.


Understanding the Challenge

ServiceNow users often need validation logic in catalog items or forms to prevent users from selecting inappropriate dates. Specifically, you might have a scenario where the selected "end date" should not exceed one year from the current date. This validation typically involves comparing two date fields: a start date (often set as the current date) and the user-selected end date.


Step-by-Step Guide: Using Current Date in Client Scripts

Below are the steps to successfully use and validate the current date in a client-side script within ServiceNow:


Step 1: Define Current Date in Client Script

Use JavaScript's built-in Date object to obtain the current date. Format this date according to your user's date format preferences, which is essential for consistent validation.

Here's how you can achieve this:

var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_format);
  • today_date creates a new JavaScript Date object representing the current date and time.

  • today_date_str formats this date to align with your ServiceNow instance’s user date settings.


Step 2: Validate the End Date Against the Current Date

You typically need a GlideAjax call for robust validation involving server-side logic. Here's an optimized and working example for a common scenario:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        return;
    }

    var today_date = new Date();
    var today_date_str = formatDate(today_date, g_user_date_format);

    var ga = new GlideAjax('getYearAgoDate');
    ga.addParam('sysparm_name', 'validateDateDurationOneYear');
    ga.addParam('sysparm_startdate', today_date_str);
    ga.addParam('sysparm_enddate', newValue);
    ga.getXMLWait();

    var answer = ga.getAnswer();

    if (answer === true || answer === 'true') {
        g_form.hideFieldMsg('contractor_end_date', true);
    }
    else {
        g_form.clearValue('contractor_end_date');
        g_form.showFieldMsg('contractor_end_date', 'Maximum account duration is one year from the processing start date', 'error', true);
    }
}

In the above example:

  • GlideAjax calls a server-side script to validate whether the chosen end date exceeds one year from the current date.

  • If validation fails, a clear error message informs users of the constraint, enhancing usability.


Common Issues and Troubleshooting

  • Incorrect Date Formats: Ensure the date is always formatted consistently with user preferences using g_user_date_format.

  • Asynchronous vs. Synchronous Calls: Using getXMLWait() ensures synchronous validation, but for better user experience, consider asynchronous alternatives (getXML) if the scenario permits.

  • GlideAjax Misconfigurations: Confirm that your script includes correct server-side scripts and method references, like 'getYearAgoDate' and 'validateDateDurationOneYear'.


Alternative Solutions

While client-side validation works effectively, another approach includes business rules (server-side validation) for enhanced reliability, particularly in security-sensitive applications.


Conclusion

Leveraging client-side scripts for date validation significantly enhances your ServiceNow applications, ensuring robust data entry and streamlined workflows. By following this straightforward method to acquire and utilize the current date, developers can reliably prevent users from selecting invalid dates, improving overall data integrity and user experience.


Actionable Next Steps

  • Review your current catalog items or forms for date validations.

  • Implement this client-side validation technique as described.

  • Test the validations thoroughly in different date formats and user locales to ensure seamless operation.

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