- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2020 11:16 PM
Hello,
We have 2 fields in a form.
1. Expected Start Date/Time
2. Expected End Date/Time
The above 2 fields show the system date/time.
I want 2 more fields below below the above mentioned fields with name :
1. Expected Start Date/Time (EST)
2. Expected End Date/Time (EST)
Requirement :
Expected Start Date/Time (EST) will be copied from Expected Start Date/Time once the user fill this field and show EST time.
Expected End Date/Time (EST) will be copied from Expected End Date/Time once the user fill this field and show EST time.
Can someone please help in how to achieve this functionality?
NOTE : The fields are in scoped application.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2021 11:57 PM
Okay.
Hi create a SI in global scope and then call that Si from your client script. Then you can use the following code to convert your date/time to EST format.
Script Include
getConvertedDate: function() {
var mydate = new GlideDateTime(this.getParameter('sysparm_date').toString());
var gdt1 = new GlideDateTime();
//sets to EST
var tz = Packages.java.util.TimeZone.getTimeZone("EST");
gdt1.setTZ(tz);
gdt1.setValue(mydate);
return gdt1.getDisplayValue(); //returns the date/time in EST timezone as a string.
},
Mark the comment as a correct answer and also helpful if this helps to solve the problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2021 11:25 PM
Hi Ankur,
Can you please tell me the workaround for this?
I have tried creating both the Script Includes 'ClientTimeZoneUtils' and 'TimeZoneUtils' in Global scope and tried calling 'ClientTimeZoneUtils' script include from my client script which is in scoped application but when I am trying to make alert for answer it is printing null.
Can you please help me in rectifying the issue and populate 'expected_start_date_time_est' field on change of 'expected_start_date_time' field?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2021 11:39 PM
Hi,
you can have your script include created in Global scope and Accessible from All Application scopes
Then you can call it from your onChange client script which is Custom scope
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2021 11:57 PM
Hi Ankur,
Thanks. It is working when I have created my script includes in global and calling them from client script which is in scoped application. But the issue is it is populating current time but not converting the time which the user selected in 'expected_start_date_time' field.
For example :
I have selected time for 'expected_start_date_time' as '2021-01-20 18:00:00'.
I want the above date/time to be converted but it shows current time in 'expected_start_date_time_est' field.
Note : I want it in EST but tried in India for checking if it is working fine or not.
Below are the script includes I have used :
Script Include : TimeZoneUtils
var TimeZoneUtils = Class.create();
TimeZoneUtils.prototype = {
initialize: function(gdt) {
this.gdt = (typeof gdt == 'undefined') ? (new GlideDateTime()) : gdt;
},
getGDT: function() {
return this.gdt;
},
getOffsetHours: function() {
return ((Number(this.gdt.getTZOffset() / 1000) / 60) / 60);
},
setTimeZone: function(tz) {
//ensure we've got a string and that it's lower-case.
tz = (typeof tz === 'string') ? tz : tz.toString();
//Validate the TZ string, and get a TimeZone Java object from it.
tz = this._getTimeZoneFromString(tz);
this.gdt.setTZ(tz);
return this.gdt;
},
getDisplayValue: function() {
return this.gdt.getDisplayValue();
},
getValue: function() {
return this.gdt.getValue();
},
_getTimeZoneFromString: function(tz) {
//If it's a valid time-zone coming in, bob's our uncle.
if (this._isValidTimeZone(tz)) {
if (this.timeZones.hasOwnProperty(tz.toLowerCase())) {
return this.timeZones[tz.toLowerCase()];
} else {
return Packages.java.util.TimeZone.getTimeZone(tz);
}
}
//Otherwise, check if it matches one of our timeZone object properties.
var shortTZ = this._getShortTimeZoneName(tz);
if (this._isValidTimeZone(shortTZ)) {
return this.timeZones[shortTZ.toLowerCase()];
}
//If nothing else has returned by now, it means the time zone isn't valid.
gs.warn('Invalid time zone specified. Time zone: ' + tz, 'TimeZoneUtils Script Include, _getTimeZoneFromString method');
return false;
},
_isValidTimeZone: function(tz) {
var tzObj = Packages.java.util.TimeZone.getTimeZone(tz);
//If the tz string wasn't valid, then getID will return the string "GMT",
//which - unless the user specified GMT as the time-zone, will not match the string argument.
//However, if it does match, OR if the arg is found in the timeZones object, then we're good to go.
return ((String(tzObj.getID()) === tz) || this.timeZones.hasOwnProperty(tz.toLowerCase()));
},
_getShortTimeZoneName: function(tz) {
//Check if the string contains a forward-slash, back-slash, or underscore.
if (tz.indexOf('\\') >= 0 || tz.indexOf('/') >= 0 || tz.indexOf(' ') >= 0) {
tz = tz.slice(tz.indexOf('\\') + 1).slice(tz.indexOf('/') + 1).trim().replace(/ /g, '_');
}
return tz.toLowerCase();
},
setTZ: function(tz) {
return this.setTimeZone(tz);
},
timeZones: {
alaska: Packages.java.util.TimeZone.getTimeZone('US/Alaska'),
eastern: Packages.java.util.TimeZone.getTimeZone('US/Eastern'),
central: Packages.java.util.TimeZone.getTimeZone('US/Central'),
mountain: Packages.java.util.TimeZone.getTimeZone('US/Mountain'),
hawaii: Packages.java.util.TimeZone.getTimeZone('US/Hawaii'),
pacific: Packages.java.util.TimeZone.getTimeZone('US/Pacific'),
arizona: Packages.java.util.TimeZone.getTimeZone('US/Arizona'),
guam: Packages.java.util.TimeZone.getTimeZone('Pacific/Guam'),
puerto_rico: Packages.java.util.TimeZone.getTimeZone('America/Puerto_Rico'),
india: Packages.java.util.TimeZone.getTimeZone('Asia/Kolkata'),
utc: Packages.java.util.TimeZone.getTimeZone('UTC')
},
type: 'TimeZoneUtils'
};
Script Include : ClientTimeZoneUtils
Client Script : TimeZone EST
Can you please let me know where I am going wrong?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2021 12:11 AM
Hi,
You can have this simple function to get time in EST
Asif has shared the approach please check that
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2021 10:31 PM
Hi,
If there is only a static list of timezones that you wanted to try then run this code in background script and get the output of these lines in that array
gs.print(Packages.java.util.TimeZone.getTimeZone('US/Alaska'));
Mark the comment as a correct answer and also helpful if it helps.