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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2018 04:36 AM
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()
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().
- 7,793 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2018 04:42 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2018 05:09 AM
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2018 04:02 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2018 09:55 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader