Working with Dates in ServiceNow can be quite challenging.

In this Blog Series, I will list some of the common mistakes and provide some secrets to using GlideDateTime that you might not be aware of.

This will be updated on an ongoing basis.



1) Date Formatting

2) Scheduled Tasks using Date GlideDate

Calculating Business Duration In Schedule [COMING SOON]


1) Date Formatting


Date formatting is actually incredibly easy in ServiceNow. I have seen lots of posts with complex code to format dates into day names, month names and everything in between - often with 10's of 100's of lines of code. But it doesn't have to be this hard! It is actually very easy. You can do all your formatting using one function, provided OOTB!!/api_doc?v=jakarta&id=r_ScopedGlideDateTimeSetDisplayValue_String_value_String_…



var gdt = new GlideDateTime("2011-02-02 12:00:00");
gdt.setDisplayValue("20-5-2011 12:00:00", "dd-MM-yyyy HH:mm:ss"); //uses current user session time zone (US/Pacific)


setDisplayValue() uses the SimpleDateFormat (Java Platform SE 7 ) , which is fully documented!


Converting Formats

Incoming Format: 20171216 01:23:45

Simple Format: yyyyMMdd HH:mm:ss


var date = '20171216 01:23:45';
var simpleDateFormat = 'yyyyMMdd HH:mm:ss';
var gdt = new GlideDateTime();

2017-12-16 01:23:45


Converting Day and Month Names with AM/PM

Incoming Format: Friday, August 18, 2017 8:00 AM

Simple Format: E, MMMM dd, yyyy K:mm a


var newDate =  'Friday, August 18, 2017 8:00 AM';   
var gdt = new GlideDateTime();   
gdt.setDisplayValue(newDate, "E, MMMM dd, yyyy K:mm a");   
var dateTimeForField = gdt.getDisplayValue();  


2017-08-18 08:00:00


Getting the Name and Day of Month

Incoming Format: new GlideDateTime();

Simple Format: EEEE dd MMMMM


var gDate = new GlideDateTime().getDate();
gs.print(gDate.getByFormat('EEEE dd MMMMM'));


Thursday 14 December


2) Scheduled Tasks using Date GlideDate


It is a fairly common requirement - generate a task every day of the week, excluding weekend.

If you do a quick search for this, you will see lots of versions of the code below which uses Date():


(function doNotUseThisCode() {
var now = new Date();
var day = now.getDay();
var result = false;
if(day != 0 && day != 6) {
result = true;
return result;


Please do not use this code! It is wrong!


If you run the code below (run from a Perth Instance in Australia)


gs.addInfoMessage(new Date().toString());
gs.addInfoMessage(new GlideDateTime().getDisplayValue());


You get the following output:

Tue Jan 02 2018 05:38:49 GMT-0800 (PST)

02-01-2018 21:38:49

What is going on here?


The output of Date().toString() is not using your TimeZone!


now.getDay() is getting the day in PST!


I am from Perth, Australia (GMT + 8) which means that my Task will spawn on Saturday and not on Monday because it is still Friday and Sunday retrospectively in PST.

And you can bet that is what happened when the code went into Production... :/


Let's utilise what we learnt from Date Formatting for a more readable and TimeZone agnostic solution using GlideDate instead:


(function isWeekday() {
  var isWeekday;
var gDate = new GlideDate(); //Uses your Timezone!
var day = gDate.getByFormat('EEEE'); //Gets name of day
switch(day) {
    case "Saturday":
    case "Sunday":
      isWeekday = false;
      isWeekday = true;
return isWeekday;