Date/Time validation for Date/Time variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2023 10:44 PM - edited 10-19-2023 10:45 PM
I have 2 variables which are start_date_time and end_date_time for service catalogue
Both variables have Date/Time type. FYI, I am in Australia.
My script won't work for the following scenario. Could you please help?
- if start_date_time is less than a minute ago, clear value of end_date_time.
- if the start_date_time is today and in the past, clear value of end_date_time
- if the end_date_time is older than start_date_time, clear start_date_time
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var startDateTime = g_form.getValue('start_date_time');
var endDateTime = g_form.getValue('end_date_time');
var currentDateTime = new GlideDateTime();
// Check if the start_date_time is in the past
if (startDateTime < currentDateTime) {
g_form.clearValue('start_date_time');
alert("Start date and time cannot be in the past.");
return;
}
// Check if the start_date_time is today and in the past
if (startDateTime.equals(currentDateTime) && g_form.getValue('start_date_time') <= currentDateTime) {
g_form.clearValue('start_date_time');
alert("Start time cannot be the current time or in the past for today.");
return;
}
// Check if end_date_time is older than start_date_time
if (endDateTime < startDateTime) {
g_form.clearValue('start_date_time');
alert("Start date and time cannot be later than the end date and time.");
return;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2023 10:53 PM
Hi @Kifrez ,
U can use UI policy here instead of client script & write the script part for clearance & alert when condition matches to true.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2023 10:57 PM
Hello @Kifrez ,
Remove last three return; from your code.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var startDateTime = g_form.getValue('start_date_time');
var endDateTime = g_form.getValue('end_date_time');
var currentDateTime = new GlideDateTime();
// Check if the start_date_time is in the past
if (startDateTime < currentDateTime) {
g_form.clearValue('start_date_time');
alert("Start date and time cannot be in the past.");
}
// Check if the start_date_time is today and in the past
if (startDateTime.equals(currentDateTime) && g_form.getValue('start_date_time') <= currentDateTime) {
g_form.clearValue('start_date_time');
alert("Start time cannot be the current time or in the past for today.");
}
// Check if end_date_time is older than start_date_time
if (endDateTime < startDateTime) {
g_form.clearValue('start_date_time');
alert("Start date and time cannot be later than the end date and time.");
}
}
Please mark my answer as helpful and solution accepted.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 12:07 AM
Hi @Kifrez ,
Check if start_date_time is in the past or less than a minute ago.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var startDateTime = g_form.getValue('start_date_time');
var endDateTime = g_form.getValue('end_date_time');
var currentDateTime = new GlideDateTime();
// Check if start_date_time is in the past or less than a minute ago
var oneMinuteAgo = new GlideDateTime();
oneMinuteAgo.addSeconds(-60);
if (startDateTime < oneMinuteAgo) {
g_form.clearValue('end_date_time');
alert("Start date and time cannot be in the past or less than a minute ago.");
return;
}
// Check if start_date_time is today and in the past
if (startDateTime.equals(currentDateTime) && g_form.getValue('start_date_time') <= currentDateTime) {
g_form.clearValue('start_date_time');
alert("Start date and time cannot be in the past.");
return;
}
// Check if end_date_time is older than start_date_time
if (endDateTime < startDateTime) {
g_form.clearValue('start_date_time');
alert("Start date and time cannot be later than the end date and time.");
}
}
Please mark it as solution proposed and helpful if it works for you.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 12:17 AM
Hi @Kifrez ,
I missed the part that you are using GlideDateTime in client script which does not work over there. Kindly use GlideAjax to validate the condition & return the result as a response from there.
Thanks,
Danish