The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Date Format in Script Output

Mike McCall
Mega Guru

Is there an easy way to script date fields into whatever format(s) I want?

/* The Created field prints as "2015-10-27 19:52:33" (but I want the time to show 7:52 PM). */

gs.print(current.sys_created_on);

/* Running the field through GlideDateTime gets me to the same output... */

var glideDateTime = new GlideDateTime(current.sys_created_on);

gs.print(glideDateTime.getValue()); // This also returns "2015-10-27 19:52:33"

/* ...so, NOW WHAT? */

In the past, I've done painful translations on my own to create a string by pulling out individual elements and putting them back in the order I want. I know I could reuse this work through something like a Script Include...but has all that already been done for me?

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You might check out this post:


Setting Date-Time fields on forms



Why do you need a specific format that is different than the logged in user's date/time format?


I ultimately ended up building my own "formatDate" method into a new Script Include that accepts a specific format string (e.g., "yyyy-MM-dd hh:mm a") and outputs the value of a GlideDateTime object accordingly.



In my use case, I was pulling a start date variable into the Short Description of a Requested Item, and I wanted all of the records to be uniform (regardless of the requesting user's system-wide format selection).


Kalaiarasan Pus
Giga Sage

It looks like your line in the linked post already accomplished what I ended up re-coding:


new Packages.java.text.SimpleDateFormat("EEEE,MMMM dd,yyyy")



Two questions, then:


  1. Should I be avoiding Packages whenever possible?
  2. Is there a place I can see the available methods, etc.?