How to get the current time in the client script as per the user's timezone in system settings. The current time differs if the user's timezone is US/Pacific vs user's timezone is Asia/Kolkata

Santosh Vaddadi
ServiceNow Employee
ServiceNow Employee

This is more of a knowledge sharing rather than a question as we spent considerable time searching for the answer in the community and found no useful results.

In short if you are looking to get the current time in user's time zone the function to be used is getUserDateTime()

find_real_file.png

We have a requirement to populate the Valid from, Valid To fields. Each date field has onChange client scripts that check the entered date against the current date. The date pickers were showing the date in the user's time zone (in our case US/Pacific). The client script for 'Valid to' has a check to see if the date value should be greater than user's current time. 

The client script is trying to get current date using newDate().getTime() which is giving time in UTC. What we needed was a way to get the current time adjusted to user's timezone. 

The function to be used is getUserDateTime().

 

 

-Santosh Vaddadi (Sr Manager, Risk Engineering, ServiceNow)
5 REPLIES 5

Ayush Biswal1
Kilo Explorer

Hi Santosh i have a similar issue could you share the working code of your client script because for me the getUserDateTime() is not working and for now i am getting the local time while using newDate().getTime(). But i require the user time in client script.

It would be a great help if you could reply promptly in case you have the code.