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

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

 

You have to convert this date by using below code:

var gr = new GlideDateTime(Date);

 

Now the question is , is this date only or date time both as well as is this In same format as system uses. If no then we have to convert this.


Thanks,
Ashutosh Munot

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.

 

Hi Chuck,

 

One quick question. If the date is coming in different format, what can we do?

 

Thanks,
Ashutosh Munot

My recommendation is to use a date or date/time field to allow them to pick. Then the format is known.

 

GlideDateTime() is pretty good about understanding the known formats within ServiceNow. If you get outside of that (using dots instead of dashes for example) then you're going to have issues. I definitely would not leave the input as a string field or you'll never get good coverage on the parsing.