1

I want to fetch record from the sys_user table which is updated at or after certain time stamp. for that I have created rest request as

https:/service-now.com/api/now/v1//table/sys_user?sysparm_query=sys_updated_on>=javascript:gs.dateGenerate('2017-10-30','01:25:00')

I had converted current time which is in IST format into GMT and pass it to dateGenerate() function.

Problem statement - I don't want to convert the IST to GMT, is there any way by which i can identify ServiceNow instance time zone at runtime and convert given time into that time stamp and get the users. If i can pass this date and time in UTC format.

Anand Dhage
  • 109
  • 17

1 Answers1

0

Ahoy!

This is a great question, and something that's quite difficult in ServiceNow (dealing with time-zones). As such, I've written a tool to manage this for you. It's totally free!

The tool is called TimeZoneUtil, and can be found here: https://snprotips.com/blog/2017/9/12/handling-timezones-in-servicenow-timezoneutil

You simply need to initialize a GlideDateTime object, set its' time-zone to IST, use setDisplayValue() to set its' time based on IST current time, then use .getValue() to get that same time in system time. This is because getDisplayValue()/setDisplayValue() work based on time-zone, whereas setValue()/getValue() always return the corresponding system time.


EDIT: In order to make this a little more clear, I'll provide some example usage below.

var tz = new TimeZoneUtils(); //initialize with current time
gs.print(tz.getOffsetHours()); //prints out "-7" in my instance, as the system time is in Pacific.
tz.setTimeZone('Asia/Kolkata'); //sets the time-zone to IST/UTC+5.5
gs.print(tz.getOffsetHours()); //prints "5.5".
gs.print(tz.getGDT().getDisplayValue()); //Prints the current time in IST (2017-11-01 20:52:31 at the moment).
gs.print(tz.getGDT().getValue()); //Prints the current time in system time (2017-11-01 15:23:12 at present).

gs.print(new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()); //Single line, also prints current time in IST: 2017-11-01 20:52:31

The first 6 lines there, demonstrate basic usage and explain how it works. The eighth line however, demonstrates usage on a single line, which you can use inside a query string. For example:

sysparm_query=sys_updated_on>=javascript:new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()

Hope this helps!


Tim Woodruff

Author, Learning ServiceNow & Building Powerful Workflows

Owner/Founder, SN Pro Tips

Tim Woodruff
  • 600
  • 3
  • 9
  • Hi Tim, Thank you for your reply. i had wriiten below script var dt1 = new GlideDateTime(); gs.info("UTC date and time: " + dt1.getValue()); gs.info("GMT-7 hh - date and time: " + dt1.getDisplayValue()); this will give me servcienow instance time in GMT format. But i want to do this rest request. https:/service-now.com/api/now/v1//table/sys_user?sysparm_query=sys_updated_on>=javascript:gs.dateGenerate('2017-10-30','01:25:00') How can i embedded these two statements in this rest request? – Anand Dhage Oct 31 '17 at 05:49
  • I'm not sure if I understand you. Did you read the article I wrote? It describes exactly how to use the tool. – I've updated my original comment with some usage examples that should demonstrate what you're asking, if I understand you correctly. – Tim Woodruff Nov 01 '17 at 15:38
  • Hello, Did you get a chance to try my solution? Let me know if you need any additional help. If your question is answered, please mark it as such so others can find the answer as well. – Tim Woodruff Nov 02 '17 at 15:56
  • It's nice, but, as I'm external to snow, I have alter the value before I call the query, not as part of the query. Also, in my instance, I can't really tell what time zone Snow considers me to be in. that makes it tough to use queries by date and time. – Brent Fisher Feb 13 '21 at 16:48