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

Hitoshi Ozawa
Giga Sage
Giga Sage

I've posted a client script to convert the date/time to UTC but the common way is to just pass the numeric value of date/time in the client script to Script Include.

e.g. client script parameter to pass to script include.

 ajax.addParam('sysparm_datetime', getDateFromFormat(newValue, g_user_date_time_format));

Then in the Script Include

var dateTimeNum = this.getParameter('sysparm_datetime');
dateTimeNum = parseInt(dateTimeNum);
var d = new Date(dateTimeNum);
var gdt = new GlideDateTime(d);
gs.info(gdt.getValue());  // this will be in internal date/time format in UTC

Martin Ivanov
Giga Sage
Giga Sage

Hi. If your issue is resolved and my response has helped, plese consider marking 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

scottl
Kilo Sage

Another answer marked as correct, when it does not answer the question. And also linking to missing content (SN redirect laziness when migrating to another site).  

There is clearly a fault in SN's DateTime component on the portal, as it should not be returning the DisplayValue, as this also causes issues in order guides, and trying to pass a date field, into a Date/Time field like due_date.