Limiting time allowed between 2 date fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2013 07:55 AM
Hi guys. I have two date fields on a Service Catolog request. I want to limit the time between the two fields to 30 days. Here's the real world description:
We loan out equipment for a maximum of 30 days. The form I have created has a start and end date (variable fields). However, people can enter whatever dates they wish. I'd like to limit this to X number of days. Is this simple?
Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2013 09:53 AM
The approach to this functionality would be to create a script include which will do the date comparisons. Then you would create an onSubmit Catalog Client script that calls the script include through an Ajax call. A great article to read to get your feet wet was posted a while back:
Client Script Date/Time Functions
Let us know what you come up with, or if you require additional assistance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2013 01:47 PM
Here is a script to validate using just a Client Script. It is set up for Date variables, but if you are using Date/Time variables, just replace "g_user_date_format" with "g_user_date_time_format". Also, this script assumes that your start date has already been filled out, so you will need to validate that first.
- John
//Date Variable
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('start_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
//get end date
var endDateStr = g_form.getValue('end_date');;
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
var diff = endDateNum - startDateNum;
var maxDiff = 30*24*60*60*1000; //30 days * 24 hrs * 60 mins * 60 secs * 1000 ms
if (endDateNum <= 0){
alert('Please use the calendar icon to select a date.');
g_form.setValue('end_date', '');
} else if (endDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('end_date', '');
} else if (endDateNum < startDateNum) {
alert('You cannot select an end date prior to the start date.');
g_form.setValue('end_date', '');
} else if (diff > maxDiff) {
alert('You cannot select a date more than 30 days after the start date.');
g_form.setValue('end_date', '');
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2015 01:51 PM
Thanks John - this was just the solution I needed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2018 03:42 AM
Hi John,
i have used the below script for my start/end date validation in my Helsinki . It is working as expected but only thing is that it is not accepting today's date. It gives the same error : Start date cannot be in the past. I assume we need to do some small changes.
Below is my On change Client script for start date:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var currentDateNum;
var startDateNum;
var endDateNum;
g_form.hideFieldMsg('u_security_start_date');
g_form.hideFieldMsg('u_security_end_date');
if (typeof (moment) === "undefined") {
//Get start date
var startDateStr = g_form.getValue('u_security_start_date');
startDateNum = getDateFromFormat(startDateStr, g_user_date_time_format);
//Get end date
var endDateStr = g_form.getValue('u_security_end_date');
endDateNum = getDateFromFormat(endDateStr, g_user_date_time_format);
//Get current date
var currentDateObj = new GlideDate();
var currentDateStr = formatDate(currentDateObj, g_user_date_time_format);
currentDateNum = getDateFromFormat(currentDateStr, g_user_date_time_format);
}
else
{
// current date
currentDateNum = moment().valueOf();
//get start date
startDateNum = moment(g_form.getValue('u_security_start_date'));
//get end date
endDateNum = moment(g_form.getValue('u_security_end_date'));
}
if(newValue == '')
{
return;
}
if(endDateNum!='')
{
if (endDateNum < startDateNum)
{
g_form.hideFieldMsg('u_security_start_date');
g_form.showFieldMsg('u_security_start_date', 'Start date must be before the End date','error');
g_form.setValue('u_security_start_date','');
return false;
}
}
//if start is past date
if (startDateNum < currentDateNum) {
g_form.hideFieldMsg('u_security_start_date');
g_form.showFieldMsg('u_security_start_date', 'The Start Date cannot be in the past','error');
g_form.setValue('u_security_start_date','');
return false;
}
}
It gives the below error for today's date as well.. I want to able to select the current date. Can you pls help???
Thanks