- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-22-2020 11:31 PM
Hello Experts,
I have a custom field on the sys_user table called 'Date TZ', which is a Date/Time type.
I have a requirement where I need to show the time in this field, what ever the user chooses from the 'Time Zone' field.
For Example: if the user chooses 'Europe/London', the 'Date TZ' should show the time in 'Europe/London', as of now it shows in the default system time.
Is there a way to do this ?
Thanks
Veer
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2020 11:29 PM
Hi,
I got this working with a script include and a onchange client script.
Script Include:
var DateTimeZone = Class.create();
DateTimeZone.prototype = Object.extendsObject(AbstractAjaxProcessor, {
setDateTimeZone: function() {
var timez = this.getParameter('sysparm_time_zone');
var tz = Packages.java.util.TimeZone.getTimeZone(timez);
var gr = new GlideRecord("sys_user");
gr.addActiveQuery();
gr.query();
if (gr.next()) {
var time = new GlideDateTime();
time.setTZ(tz);
var timeZoneOffset = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffset);
return time;
}
},
type: 'DateTimeZone'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var timezone = g_form.getValue('time_zone');
var ga = new GlideAjax('DateTimeZone');
ga.addParam('sysparm_name', 'setDateTimeZone');
ga.addParam('sysparm_time_zone', timezone);
ga.getXML(getDateTimeZone);
function getDateTimeZone(response) {
var answer = response.responseXML.documentElement.getAttribute("answer").toString();
g_form.setValue('u_date_tz', answer);
}
//Type appropriate comment here, and begin script below
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 01:29 AM
Hi Veer,
I have modified it a bit,I didn't get time to check it on my instance but hoep it would help you.
// Get date from service now
var time = new GlideDateTime(current.date_time_field);
// Set timezone
var tz = Packages.java.util.TimeZone.getTimeZone(current.time_zone_field);
time.setTZ(tz);
// Get offset of timezone
var timeZoneOffSet = time.getTZOffset();
// Add offset to time field
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
current.date_time_field = time;
Regards,
Munender

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 01:15 AM
Also go through the below link -
https://snprotips.com/blog/2017/9/12/handling-timezones-in-servicenow-timezoneutil
Hope this helps.
Regards
Omkar Mone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2020 11:29 PM
Hi,
I got this working with a script include and a onchange client script.
Script Include:
var DateTimeZone = Class.create();
DateTimeZone.prototype = Object.extendsObject(AbstractAjaxProcessor, {
setDateTimeZone: function() {
var timez = this.getParameter('sysparm_time_zone');
var tz = Packages.java.util.TimeZone.getTimeZone(timez);
var gr = new GlideRecord("sys_user");
gr.addActiveQuery();
gr.query();
if (gr.next()) {
var time = new GlideDateTime();
time.setTZ(tz);
var timeZoneOffset = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffset);
return time;
}
},
type: 'DateTimeZone'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var timezone = g_form.getValue('time_zone');
var ga = new GlideAjax('DateTimeZone');
ga.addParam('sysparm_name', 'setDateTimeZone');
ga.addParam('sysparm_time_zone', timezone);
ga.getXML(getDateTimeZone);
function getDateTimeZone(response) {
var answer = response.responseXML.documentElement.getAttribute("answer").toString();
g_form.setValue('u_date_tz', answer);
}
//Type appropriate comment here, and begin script below
}