Display timezone code in mail script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2020 02:44 AM
Hi
I have a field which is showing a time in a particular time zone using the code below:
var tz = Packages.java.util.TimeZone.getTimeZone("Asia/Manila");
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
I am using the content of this field in an email script which populates the email client to provide a time and date in that specific timezone.
This is working fine but there are a few formatting issues. Firstly it shows the time/date in this format:
2020-02-19 23:16:45
Is there a way I could change the formatting to show like this in the email:
dd/mm/yy hh:mm
Also, there are many timezones displayed in the email we send out, is there a way I can get the timezone code appended (GMT, CET etc) to the end of each? so that its clear what timezone each date/time are in? I would hard code it but this wont take into account Daylight Saving Time
Any assistence would be much appreciated.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2020 02:59 AM
If you put that code into a function and run it based on the time zone on the user record (assuming you have time zone on the user record) you can dynamically set the time to display in the users time zone and include the code for that time zone. eg:
var gr = new GlideRecord('sys_user');
if(gr.get(gs.getUserID()){
var getTZ_time = getUserTime(gs.nowDateTime(), gr.time_zone);
template.print(getTZ_time + ' ' + userGR.time_zone + ' - ' + userGR.name);
//other code....
}
function getUserTime(date,usertimezone){
var timezone = Packages.java.util.TimeZone.getTimeZone(usertimezone);
var gdt = new GlideDateTime(date);
gdt.setTZ(timezone);
gdt.setNumericValue(gdt.getNumericValue() + gdt.getTZOffset());
return gdt;
}
As for the format, it should display in whatever is defined in your system preferences, if you want to deviate from that you could play around with the GlideDate getByFormat() method.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2020 03:08 AM
Hi David
Thanks for the amazingly quick reply!
Unfortuantely the field is not based on user timezone, it needs to be a fixed timezone.
Can the getByFormat be used to display the timezone code as well? (ie dd/MM/yyy zzz)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2020 03:34 AM
You can modify the function to pass in the time zone from wherever it is, it doesn't have to be from the user record. If it's from a field on a form or something you can pass that in instead.
The getByFormat() method only works on dates so you'd need to use getDate() to get the date portion of the GlideDateTime variable, reformat it and then recombine it with the time portion. There might be better ways to do this, i'd recommend trawling the community for reformatting glidedatetime articles!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2020 03:39 AM
i think this would be helpful 🙂 sample code has been added on below blog.
https://community.servicenow.com/community?id=community_blog&sys_id=bc0e6a2ddbd0dbc01dcaf3231f961931