- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 11:22 AM
g_form.getValue('start_date') returns "03/31/2022 01:00:00 PM" and this is causing issues when I pass that value to a script include for verification against other dates. I am in CST, so the UTC value that the script include uses should be "2022-03-31 18:00:00". Can I achieve this easily with GlideDateTime or another API or do I have to figure out how to manually re format that date?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 06:10 PM
Hi,
When you pass this "display value" to your script include, you'd want to use a GlideDateTime object within the SI and set the display value of that object to the display value brought over from your GlideAjax.
Then, you can use the server value (.getValue()) as needed within that GlideDateTime object which will give you it's UTC.
There's no need for any conversion, long scripts, etc.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 12:21 PM
Hi. Please find this very useful custom script for timezone handling.
Check also the docs of GlideDateTime. Some methods might be useful for your case.
Please Mark Correct AND Helpful. Thanks!
Martin Ivanov
2022 Community Rising Star
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 05:45 PM
Below script will convert the date/time in local format to UTC in yyyy-MM-dd hh:mm:ss format.
I have field "datetime" of type DateTime and field "utcdatetime" of type Single Line Text. The script will output the time in UTC in utcdatetime.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var date = new Date(getDateFromFormat(newValue, g_user_date_time_format));
var date2 = new Date(date);
var utcDate = date2.getUTCFullYear() + '-' + padZero(date2.getUTCMonth() + 1) + '-' + padZero(date2.getUTCDate()) + ' ' + padZero(date2.getUTCHours()) + ":" + padZero(date2.getUTCMinutes()) + ":" + padZero(date2.getUTCSeconds());
g_form.setValue('utcdatetime', utcDate);
}
function padZero(num) {
return ('00' + num).slice(-2);
}
Execution. I'm in JST (+9) so the UTC time will be 9 hours back.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 06:06 PM
Hi,
Using .getValue() would get you the server value, which is stored in UTC.
Using .getDisplayValue() would get you the display value of the field, which is set to your preference and timezone.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 06:10 PM
Hi,
When you pass this "display value" to your script include, you'd want to use a GlideDateTime object within the SI and set the display value of that object to the display value brought over from your GlideAjax.
Then, you can use the server value (.getValue()) as needed within that GlideDateTime object which will give you it's UTC.
There's no need for any conversion, long scripts, etc.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!