How to convert local time to system time?

kiki330611
Kilo Expert

Hi Guys,

I have a GlideRecord script similar to below:

=====

var gr = new GlideRecord('incident');

gr.addQuery('sys_created_on', '>', Date)

.....

=====

The Date variable will come from an api call as a parameter sent by the client. Basically they want to know how many records were created at a certain range of dates. But they will send the date/time in their local time. How do I convert their local time to ServiceNow's system time (UTC) so I can put that date into the addQuery?

Many thanks!

 

 

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

There are a couple things to remember when doing date/time operations.

  1. The system stores all date/time fields as UTC - or at least it believes it does.
  2. If you want to apply your local to the value retrieved from a field, use getDisplayValue(). 
  3. If you want UTC, use getValue()
  4. 2 and 3 also apply to setDisplayValue() and setValue(). 

Once you keep it straight in your head when a TZ is applied (e.g. display values) and when it is not (getValue()/setValue()) everything else becomes pretty easy.

 

Based your requirement, you want to take the value the user gave you, and strip the timezone - something like this:

 

// change user.date to whatever variable the user gave you

var gr = new GlideRecord('incident');

var date = new GlideDateTime();

date.setDisplayValue(user.date); // insert it in to the variable with TZ applied, so the internal value has no TZ

gr.addQuery('sys_created_on', '>', date);

 

Standard disclaimer: The following code is untested, requires review and potential modifications.

 

View solution in original post

5 REPLIES 5

Thanks Chuck! That solved my problem!