
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2019 07:57 AM
I am trying to restrict a date picker on the portal.
I want to stop a user selecting a "finish date" greater than the date selected in the "start date".
I am using the following:
With the following script:
function onCondition() {
alert('Finish date cannot be longer than 12 weeks from the start date');
g_form.setValue('finish_date','');
}
But this doesnt appear to work. Is this due to the condition? If so, is there a better way of doing this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 08:26 AM
i am going to close this thread as direction now different to original path. I will re submit a new thread as the issue appears to be setting the JS Date object in the portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 04:56 AM
Should work... After user saves the form the "finish_date" inserted, so you may want to hide or make read-only "finish_date" field unless it's populated (using UI Policy + UI Policy Action) in order not to confuse them.
(function executeRule(current, previous /*null when async*/) {
// The startDate stores the date users pick
var startDate = new GlideDateTime(current.start_date);
// Add 12 weeks from the date users pick
startDate.addWeeksLocalTime(12);
// Assign the calculated date to another field
current.finish_date = startDate.getDate();
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 05:04 AM
the finish date can be picked by the user - 12 weeks is the maximum.
I would prefer an onchange script so that it clears the value and gives an alert. Something like the below.
However, I get an error on the portal: ReferenceError: formatDate is not defined
I cant figure it out
//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.toUpperCase());
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format.toUpperCase());
//get start date
var startDateStr = g_form.getValue('start_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format.toUpperCase());
//get end date
var endDateStr = g_form.getValue('finish_date');
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format.toUpperCase());
var diff = endDateNum - startDateNum;
var maxDiff = 84*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('finish_date', '');
} else if (endDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('finish_date', '');
} else if (endDateNum < startDateNum) {
alert('You cannot select an end date prior to the start date.');
g_form.setValue('finish_date', '');
} else if (diff > maxDiff) {
alert('You cannot select a date more than 12 weeks after the start date.');
g_form.setValue('finish_date', '');
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 06:22 AM
Try to replace currentDateStr in the line #8 with currentDateObj. currentDateStr is not defined anywhere in the code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 06:52 AM
Its formatDate and getDateFromFormat that doesnt appear to be defined?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 07:07 AM
I dont actually need those top vars and associated alerts so have commented out.
It seems to definitely be getDateFromFormat that appears to be the issue:
//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('finish_date');
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
var diff = endDateNum - startDateNum;
var maxDiff = 84*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('finish_date', '');
// } else if (endDateNum < currentDateNum) {
// alert('You cannot select a date in the past.');
// g_form.setValue('finish_date', '');
} else if (endDateNum < startDateNum) {
alert('You cannot select an end date prior to the start date.');
g_form.setValue('finish_date', '');
} else if (diff > maxDiff) {
alert('You cannot select a date more than 12 weeks after the start date.');
g_form.setValue('finish_date', '');
}
}
}
}