Client Side Dates in ServiceNow

///Client Side Dates in ServiceNow

Client Side Dates in ServiceNow

Dates on the client side in ServiceNow have always been challenging. Javascript has its own complexities in this regard and mixing this with ServiceNow’s date formats and user preferences can make it a nightmare.

Here we walk through some of the methods of working with client side dates that make it MUCH simpler and allow us to avoid the situation of having to return to the server via GlideAjax for most, if not all, date calculations.

ServiceNow Date Formats drop-down

If you’re here as a reference and just need the key code snippets, here you go. Basically, to get a javascript date from a ServiceNow date field value do this:

var date_number = getDateFromFormat(g_form.getValue('the_date_field'), g_user_date_format);
var my_date = new Date(date_number);

Or, from a Date/Time field do this:

var date_number = getDateFromFormat(g_form.getValue('the_date_time_field'), g_user_date_time_format);
var my_date = new Date(date_number);

The Details

Working with dates on the client side of things in ServiceNow has always been a challenge. Part of this is just due to Javascript and the challenges associated with dates and the other is ServiceNow. Given that users can have their own date format makes it even more challenging. That said, we are given some tools (albeit undocumented ones) that can help improve the situation.

I ran across some information a while back (thanks to John Roberts) that helps the situation drastically. The key comes down to a couple global variables defined by the system as well as function that helps use those variables.

The variables give us the date and datetime formats for the logged in user, and the function lets us use those to get something we can work with a little easier.

User datetime format: g_user_date_time_format
User date format: g_user_date_format

These used with the getDateFromFormat function gives us an easy way to get a value for the date (essentially a Unix Timestamp). That value can then be used directly or immediately passed into a Javascript Date object to allow for reformatting, testing, or whatever else is needed.

Here are a few functions that I put together to simplify the date validation process that is so often needed.

Test for valid DateTime based on user format:

function isValidDateTime(value){
  if(value == '' || value == undefined || value == null){
    return false;
  }
  return(getDateFromFormat(value, g_user_date_time_format) != 0);
}

Test for valid Date based on user format:

function isValidDate(value){
  if(value == '' || value == undefined || value == null){
    return false;
  }
  return (getDateFromFormat(value, g_user_date_format) != 0);
}

To take this a step further, you could easily add comparisons of dates or add custom date formats based on the values.

One thing to keep in mind with this, depending on where the date is coming from you may still have two date formats. Any of the dates that will work as shown are going to be the values in fields on a form or the Display Value of the date. If you’re getting something from the server (e.g. via GlideAjax) as just a value then it will be stored in the regular system format of YYYY-MM-DD HH:MM:SS.

An additional example suggested by a co-worker (thank Thomas) to validate that the date is within an hour

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

  //To change the date range from 1 hour, change var minutes_from_now to desired amount time
  var minutes_from_now = 60;
  var date_obj = new Date(getDateFromFormat(newValue, g_user_date_time_format));
  var future_date = new Date(new Date().getTime() + minutes_from_now*60000);
  if(future_date < date_obj){
    alert("Time is too far out");
  }
}

Hopefully this helps save you some of the frustration I’ve experienced over the years when dealing with client side dates in ServiceNow.

By | 2017-02-24T10:53:07+00:00 February 22nd, 2017|Categories: Client scripts|Tags: , |5 Comments

About the Author:

Having led the way on numerous implementations of ServiceNow starting in 2008, James has experience across the board including traditional areas of ITSM like Incident, Problem, Service Catalog, and Configuration Management as well as Content Management System (CMS) sites Service Portal, HR, and more. He has education background in IT and has held a variety of positions giving him a broad perspective on how things work and the ability to handle the details while still looking at the big picture.

5 Comments

  1. Jawaharlalnehru Vaiyapuri February 22, 2017 at 12:09 pm - Reply

    This is really a cool cheat sheet kind of information to be handy always… when ever you are having users in multiple time zones and multiple time formats…

  2. Richard Selby February 23, 2017 at 4:49 am - Reply

    Thanks, James. Very handy.

  3. Kamal Garg May 24, 2017 at 5:42 am - Reply

    But getDateFromFormat doesnt work on service portal . is there any alternativ for it to run on service portal

    • James Farrer May 30, 2017 at 10:37 am - Reply

      Unfortunately, I haven’t found a great solution for Service Portal yet. I think it’d be a great enhancement request to ServiceNow to add support back in. If I can find something I’ll post it here.

  4. John Boyland June 22, 2017 at 9:39 pm - Reply

    Looks like a basic implementation of moment.js is available in service portal, however this only works from service portal and not the backend.

    By default the format of the date component is incorrecnt and will need to be converted to uppercase to work with moment.js. You could do something like this which should work.

    var dateFormat = g_user_date_format.toUpperCase();
    // get the full date time format
    var dateTimeFormat =g_user_date_time_format;
    // construct correct date time format where we strip the date and use the upcase version
    var newDateTimeFormat = dateFormat + dateTimeFormat.substring(dateFormat.length);

    //create date object using moments and correct date time format
    var dateObj = moment(g_form.getValue(‘the_date_field’), newDateTimeFormat);

Leave A Comment