- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2022 11:30 PM
Hi everyone,
I need to create a date/time type variable.
And in the date picker, the date should be only future dates and allow only upto 90 days.
Thanks in advance for your help!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 05:01 AM
Hi @Southsayer ,
Then you can use the 2 onchange client scripts.
1. Create onchange client script on start date and use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
var start = new Date(g_form.getValue('start_date')); //your start date variable name
var end = new Date(g_form.getValue('end_date')); //your end date variable name
if (start > end) {
g_form.addErrorMessage('Verify start date and end date');
return false;
}
}
}
2. Create other onchange client script on end date and use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
var start = new Date(g_form.getValue('start_date')); //your start date variable name
var end = new Date(g_form.getValue('end_date')); //your end date variable name
if (end < start) {
g_form.addErrorMessage('Verify start date and end date');
return false;
}
}
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 06:46 AM - edited 10-27-2022 06:47 AM
Hi @Southsayer ,
No you need to use Onchange Client scripts only for this. Try below it will work.
1. Create onchange client script on start date and use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var endDate = new Date(g_form.getValue('end_date'));
var startDate = new Date(newValue);
if (startDate >= endDate) {
alert('start date must be before end date');
g_form.setValue('start_date', '');
}
}
2. Create other onchange client script on end date and use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var startDate = new Date(g_form.getValue('start_date'));
var endDate = new Date(newValue);
if (endDate <= startDate) {
alert('End date must be after start date');
g_form.setValue('end_date', '');
}
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2022 09:13 PM
Hi @Southsayer ,
Hope your issue got resolved close the question by Accepting solution and hit thumb icon.
Once you are issue got resolved please Accept solution and Close then only others will refer and get benefited.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar