g_form.getValue() is returns the display value of a datetime field, but I need the system (UTC) time in script include

qwertyflop
Giga Guru

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?

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

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.

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_APIRef#r_ScopedGl...

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!

View solution in original post

7 REPLIES 7

Martin Ivanov
Giga Sage
Giga Sage

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

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

find_real_file.png

Allen Andreas
Administrator
Administrator

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!

Allen Andreas
Administrator
Administrator

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.

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_APIRef#r_ScopedGl...

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!