Formatting Dates and Times

elemonnier
Tera Expert

For those that need more advanced date and time formatting and conversion methods you can use the SimpleDateFormat and TimeZone Javascript libraries to do pretty much anything you want. I needed to create some very customized e-mail templates for a change window notification.

In the following example I lookup a change ticket and pull out the start and end date. I also lookup a user to determine the user's time zone. Using the SimpleDateFormat library I can parse the Service-Now dates and then set the time zone to GMT, Service-Now's default date format. Next I create some more instances of SimpleDateFormat and TimeZone to convert the time zone and format the text how I like.

You can run this in the Background Script to tweak it until it looks right.



var gr = new GlideRecord("change_request");
gr.addQuery("sys_id","40145gvghd90004f345tgde776754");
gr.query();
while(gr.next()) {
//Find user's time zone
var ur = new GlideRecord("sys_user");
ur.addQuery("user_name","jdoe");
ur.query();
while(ur.next()) {

//Parse start and end date from change request in PST
var input = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
var tz1 = new Packages.java.util.TimeZone.getTimeZone("GMT");
input.setTimeZone(tz1);

startDate = gr.start_date;
startDate = input.parse(startDate);
endDate = gr.end_date;
endDate = input.parse(endDate);

//Convert to user's time zone and print result
var datetime = new Packages.java.text.SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm a z");
var time = new Packages.java.text.SimpleDateFormat("hh:mm a z");
var tz2 = new Packages.java.util.TimeZone.getTimeZone(ur.time_zone);
datetime.setTimeZone(tz2);
time.setTimeZone(tz2);

gs.print(datetime.format(startDate) + ' through ' + time.format(endDate));
}
}

Source dates:
startDate: 2011-10-21 05:13:49
endDate: 2011-10-21 06:13:51

Result: Thursday, October 20, 2011 11:13 PM MDT through 12:13 AM MDT

Here are the formatting options:
3 chars will give you the abbreviated text
4 or more will return the full text

LetterDate or Time ComponentPresentationExamples

G
Era designatorText

AD

y
YearYear

1996
; 

96

M
Month in yearMonth

July
; 

Jul
; 

07

w
Week in yearNumber

27

W
Week in monthNumber

2

D
Day in yearNumber

189

d
Day in monthNumber

10

F
Day of week in monthNumber

2

E
Day in weekText

Tuesday
; 

Tue

a
Am/pm markerText

PM

H
Hour in day (0-23)Number

0

k
Hour in day (1-24)Number

24

K
Hour in am/pm (0-11)Number

0

h
Hour in am/pm (1-12)Number

12

m
Minute in hourNumber

30

s
Second in minuteNumber

55

S
MillisecondNumber

978

z
Time zoneGeneral time zone

Pacific Standard Time
; 

PST
; 

GMT-08:00

Z
Time zoneRFC 822 time zone

-0800

4 REPLIES 4

lawrence_eng
Administrator
Administrator

Thank you, elemonnier!


qfawcett
Tera Contributor

If you are looking for a simple way to add the timezone indicator to ALL time or date/time fields on all forms simply modify the system property: 'glide.sys.time_format' appending a 'z' to the end of the format string.

Ex:
HH:mm:ss z

Produces date formatted as:
16:43:25 CDT


robbika
Giga Contributor

This is an old post but I was looking at it anyway.



There is an issue with notification scripting.


If you for example have some code that prints out a bunch of variables on a record, then using getDisplayValue() doesn't append the time zone info.



And there doesn't seem to be a simple way to make it do it.


emyrold
Giga Expert

Hi Eric,



Do you have any idea if this still works in Fuji?   I copy and pasted your code into scripts background.   Create a Test Change Request, updated the sys_id and the script runs.   The user's time zone is California PST but the gs.prints out the following: Thursday, November 12, 2015 08:42 PM GMT through 08:42 PM GMT     where in your example it shows MDT assuming that is what your user was configured to be.   Thanks,   -e