Date should be 2 hours later from current time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 10:49 PM
Hello all,
There is a Date and time field on a catalog item.
So, user selected time should be greater than 2 hours from the current time.
So, can you please tell me how can I achieve this?
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 10:53 PM
you can either use UI policy or onChange
Sample UI Policy
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 10:57 PM
sample onChange client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var start = g_form.getValue('variableName');
g_form.hideFieldMsg('variableName'); // give here your variable name
var minutes = 120; // 120 mins is 2 hours
var dateGiven = new Date(getDateFromFormat(start, g_user_date_time_format)).getTime() + minutes * 60000; // add 120mins to date and then compare
var nowTime = new Date().getTime();
if (dateGiven < nowTime) {
g_form.showFieldMsg('variableName', 'Please select date/time after 2 hours from now', 'error');
g_form.clearValue('variableName');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 05:59 AM
Hello Ankur,
I have used on Change CS, but even if I select 3 hours later current time, the variable is getting cleared.
Work out:
I have selected tomorrow's date first as the field is not allowing to select today's date even after changing the time. Then, I have changed the time after 3 hours from selected time and then changed the date to today's date. Then the field value is cleared and also field message is not showing.
Can you please help
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 09:35 AM
Hello @Lucky1
Taken some reference from: https://stackoverflow.com/questions/31383993/javascript-add-hours-to-time and https://stackoverflow.com/questions/37058704/getdate-slice-confusion to develop below onChange Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get the selected date and time from your date and time field
var selectedValue = g_form.getValue('select_date_and_time');
// Ensure valid date format as I am getting GMT information in the timestamp
var selectedDateTime = getDateFromFormat(selectedValue, g_user_date_time_format);
// Checking if the date is invalid (returns NaN)
if (isNaN(selectedDateTime) || !selectedDateTime) {
//console.log("Error: Invalid date format. Received:", selectedValue);
return; // Prevent further execution
}
var selectedDate = new Date(selectedDateTime);
// Add 2 hours
selectedDate.setHours(selectedDate.getHours() + 2);
// Format to exclude timezone information - function below
var formattedDateTime = formatDateTime(selectedDate);
alert(formattedDateTime); // Display formatted date-time
// Set the formatted value in the field
g_form.setValue('select_date_and_time', formattedDateTime);
}
// function to format the date correctly
function formatDateTime(date) {
var yyyy = date.getFullYear();
var mm = ('0' + (date.getMonth() + 1)).slice(-2);
var dd = ('0' + date.getDate()).slice(-2);
var hh = ('0' + date.getHours()).slice(-2);
var min = ('0' + date.getMinutes()).slice(-2);
return yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + min; // Adjust based on SN format
}
Results:
The script is working when you want to change date and time multiple times:
Hope it helps!
Hope that helps!