top of page

Experiencing challenges with ServiceNow support?

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

Displaying the Current Date in ServiceNow Client Scripts: A Practical Guide

Updated: Mar 29

As a ServiceNow developer or administrator, you often need to manipulate data and automate processes within the platform. A common requirement is to display the current date or time within a client script, triggered by user interactions or specific conditions on a form. While ServiceNow offers powerful server-side functions, directly accessing them from the client-side requires a specific approach. This article will guide you through the correct method to display the current date in your ServiceNow client scripts, ensuring accuracy and a seamless user experience.


One common mistake ServiceNow users encounter is trying to directly use the gs.nowDateTime() function within a client script. You might find yourself writing code like this, hoping to populate a date field with the current date and time:

JavaScript

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }

  if (newValue == 'false') {
    g_form.setValue("u_report_effective_as_of", gs.nowDateTime());
  }
}

However, you'll quickly discover that gs.nowDateTime() does not work in client-side scripting. This is because gs (GlideSystem) is a server-side object, and client scripts execute within the user's browser, not on the ServiceNow server. Attempting to use server-side functions directly in a client script will result in an error or unexpected behavior.


The Correct Approach: Leveraging GlideAjax

To correctly display the current date in a client script, you need to utilize GlideAjax. GlideAjax allows your client script to communicate with a server-side script include, enabling you to execute server-side logic and retrieve the desired information. Here's a step-by-step guide on how to implement this:


Step 1: Create a Client-Callable Script Include

First, you need to create a new Script Include in ServiceNow. This script include will contain the server-side logic to retrieve the current date.

  1. Navigate to System Definition > Script Includes.

  2. Click New.

  3. Give your Script Include a meaningful name (e.g., DateTimeUtils).

  4. Crucially, ensure the "Client callable" checkbox is selected. This allows client scripts to access the functions within this script include.

  5. In the "Script" field, paste the following code:

JavaScript

var DateTimeUtils = Class.create();
DateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCurrentDateTime: function() {
        return gs.nowDateTime(); // Or gs.now() for just the date
    },
    type: 'DateTimeUtils'
});
  • This script defines a class DateTimeUtils with a function getCurrentDateTime().

  • gs.nowDateTime() is used here, as it's a valid server-side function. If you only need the date, you can use gs.now().


Step 2: Modify Your Client Script

Now, you need to modify your client script to call the getCurrentDateTime() function in your newly created Script Include using GlideAjax.

JavaScript

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }

  if (newValue == 'false') {
    var ajax = new GlideAjax('DateTimeUtils'); // Replace with your Script Include name
    ajax.addParam('sysparm_name', 'getCurrentDateTime'); // Replace with your function name
    ajax.getXML(function(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
      g_form.setValue("u_report_effective_as_of", answer); // Replace with your field name
    });
  }
}
  • We create a GlideAjax object, passing the name of your Script Include (DateTimeUtils).

  • ajax.addParam('sysparm_name', 'getCurrentDateTime') specifies the function we want to call on the server-side.

  • ajax.getXML() sends the request to the server and defines a callback function to handle the response.

  • Inside the callback function, we extract the answer from the XML response and use g_form.setValue() to set the value of your desired field (u_report_effective_as_of).


Practical Example:

Imagine you have a checkbox field named u_provide_date_range. When this checkbox is unchecked (newValue is 'false'), you want to automatically populate a field called u_report_effective_as_of with the current date. The provided code snippets demonstrate exactly how to achieve this using GlideAjax.


Alternative Solutions:

While GlideAjax is the recommended approach for ensuring accuracy by fetching the server's current date and time, you could also use JavaScript's built-in Date() object within your client script. However, be aware that this will use the date and time of the user's browser, which might not always align with the ServiceNow server's time zone.

JavaScript

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }

  if (newValue == 'false') {
    var today = new Date();
    var formattedDate = today.toLocaleDateString(); // Or other formatting options
    g_form.setValue("u_report_effective_as_of", formattedDate);
  }
}

This approach is simpler but might not be suitable for all use cases where server-side date accuracy is critical.


Conclusion:

Displaying the current date in ServiceNow client scripts requires understanding the separation between client-side and server-side environments. While direct use of server-side functions like gs.nowDateTime() is not possible in client scripts, leveraging GlideAjax provides a robust and accurate solution. By creating a client-callable script include and using GlideAjax in your client script, you can seamlessly fetch the current date and time from the ServiceNow server, ensuring data integrity and a consistent user experience.


Next Steps:

  1. Identify the specific client script where you need to display the current date.

  2. Create a client-callable script include as described in Step 1.

  3. Modify your client script to use GlideAjax to call the function in your script include (Step 2).

  4. Test your implementation thoroughly to ensure it functions as expected in various scenarios.

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