logo

NJP

Validate date/time range based on logged in user's timezone

New article articles in ServiceNow Community · Jan 22, 2025 · article

Consider a requirement to validate if the user selects a time in the date/time variable between 11 AM and 4 PM.

You can use the onChange catalog client script with the following logic. It handles for every timezone.

function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } g_form.hideFieldMsg('my_datetime'); var myDate = new Date(getDateFromFormat(newValue, g_user_date_time_format)); var hourValue = myDate.getHours(); if (hourValue > 11 && hourValue < 16) { // valid } else { g_form.showFieldMsg('my_datetime', 'Time should be between 11am to 4pm', 'error'); } }

This checks the time is between 11am to 4pm based on logged in user timezone.

Output: I selected GMT & then selected Los Angeles time zone and it gave me the error based on requirement.

AnkurBawiskar_1-1737548758196.gif

View original source

https://www.servicenow.com/community/service-operations-workspace/validate-date-time-range-based-on-logged-in-user-s-timezone/ta-p/3156025