How to obtain current time and user timezone in Client Script

agarwm
Kilo Contributor

Hi,

We are looking for some date comparison validations in Client Scripts.
We are facing an issue where we need to obtain current date/time value for user. The issue is that:
1. We do not want to use Ajax calls in client scripts
2. If we obtain date/time from user's machine; is there a way to know user's ServiceNow Timezone ??

Also, is there an alternate way to compare current time with a date/time field on form. Any help will be greatly appreciated 🙂

Thanks,
Mayank

4 REPLIES 4

dada50
Kilo Explorer

if you don't want to make AJAX calls. I am wondering can you use business rule to compare the data/time. You could do an invalid update in the business rule if something is not right and display the message on top for the reason of invalid update.

current.time_zone = gs.getUser().getTZ();

Hope it helps!

Thanks, Faisal


agarwm
Kilo Contributor

Hi Faisal,

We thought about using Business Rules, but the issue is that we want to make a field mandatory based on comparison result of date, which will not be possible with BR. I guess using Ajax is the only solution, but we want to keep it as a last resort 🙂

Thanks,
Mayank


ssb
Tera Guru

You might be able to wrangle something (may not be perfect) using the JavaScript Date, e.g.
Date().getTimezoneOffset()
will get you the users' timezone offset, which won't necessarily match their ServiceNow TZ/TZ offset.

As far as I know, you can only get the users' TZ from SN with an AJAX call, which you already specified you don't want.


agarwm
Kilo Contributor

Thanks for the update. I will try the "Date().getTimezoneOffset()" fucntion and see if it works ..
Yes I guess you are right Ajax seems to be the only way .. but we wanted to avoid that since its a heavily used form and we wanted to avoid making server calls everytime it is submitted 🙂

Regards,
Mayank