Formatting Dates and Times

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2011 06:28 PM
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
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
| Era designator | Text |
|
| Year | Year | ;
|
| Month in year | Month | ; ;
|
| Week in year | Number |
|
| Week in month | Number |
|
| Day in year | Number |
|
| Day in month | Number |
|
| Day of week in month | Number |
|
| Day in week | Text | ;
|
| Am/pm marker | Text |
|
| Hour in day (0-23) | Number |
|
| Hour in day (1-24) | Number |
|
| Hour in am/pm (0-11) | Number |
|
| Hour in am/pm (1-12) | Number |
|
| Minute in hour | Number |
|
| Second in minute | Number |
|
| Millisecond | Number |
|
| Time zone | General time zone | ; ;
|
| Time zone | RFC 822 time zone |
|

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2011 09:49 AM
Thank you, elemonnier!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2012 09:29 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2014 02:42 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2015 12:51 PM
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