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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Santosh,

I think in client side you cannot do this.

 

What is your requirement?

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

vinothkumar
Tera Guru

Please check the below thread. they have done in the same way rather than using system date.

var date_number = getDateFromFormat(g_form.getValue('the_date_field'), g_user_date_format);
var my_date = new Date(date_number);

 

https://www.servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/

Santosh Vaddadi
ServiceNow Employee
ServiceNow Employee

Hi Ankur,

As mentioned already my requirement is to get the current time in the user's timezone as specified in system settings. I cant use javascript's new Date().getTime() as it always give the time in UTC.

Also i confirm that this has been tested at my end and it is working.

 

-Santosh

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

Hi Santosh,

Yes new Date().getTime() will work in client side since it is native Javascript and not servicenow specific.

What is your exact requirement?

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader