Welcome Login
Blog Photos Links

RSS Feed

Client Side Dates in ServiceNow

February 18, 2014, 9:13 am - James Farrer

TL;DR

I keep coming back to this post for specific tidbits so I decided I would put the key code snippet right at the top to make it easy. 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 Long Version

Working with dates on the client side of things in Service-Now 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 (thanks to John Roberts) that helps the situation drastically. The key comes down to a couple global variables defined by the system and a function provided 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.

Hopefully this helps save you some of the frustration I've experienced over the years.

Thomas

Example of how 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");
	}
}

Matt

Thanks, been looking all over for this. You're awesome!

Matt

FYI I believe that the function is included with this library: http://www.mattkruse.com/javascript/date/source.html I have tried other functions from there and they are also available. Pretty useful if you're working with dates a lot on the client side.

Ben Collyer

Very nice little script. I didn't realise the users date/time format was so easy to access.

Emily

Thanks James. This really worked for me.. You brought me out of my frustration

Eric

Boom! Take that, Javascript! Thanks James! (And Thomas! Your code was exactly what I needed!)

Post a Comment

Name:

To reduce spam comments, please answer the following question:
What is one plus five?

What's New

There are currently no new items, please check back later.

Archives
2017 (1)
  January (1)
2016 (4)
  December (1)
  November (1)
  May (1)
  January (1)
2015 (1)
  December (1)
2014 (2)
  August (1)
  February (1)
2013 (4)
  October (1)
  July (1)
  June (1)
  April (1)
2012 (11)
  December (2)
  October (3)
  September (1)
  May (1)
  April (1)
  February (2)
  January (1)
2011 (14)
  December (1)
  November (1)
  September (2)
  July (2)
  June (1)
  May (1)
  April (2)
  March (3)
  January (1)
2009 (2)
  October (1)
  June (1)
2008 (1)
  September (1)