
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2016 11:09 AM
I have a need to make sure that the duration between the Begin Date and End Date does not exceed 1 year.
I used scripting before to make a date go out to xxx amount of days, but never to actually do a calculation between Begin and End dates
I don't have a Due Date field. I just have the Start and End Date field and I need to make sure that Duration cannot exceed 1 year.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2016 11:22 AM
Hello Karen, its been a while hope you are well!
Here is some example code I have used before. Do you want the validation to happen on submit or as the user is inputting the data? My code below is based on an onChange of the Work End date on a task, but you could change it to an onSubmit can calculate and warn then.
Create a onChange client script for the End date:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var starts = g_form.getValue('work_start');
startMillis = getDateFromFormat(starts, g_user_date_time_format);
var ends = newValue;
endMillis = getDateFromFormat(ends, g_user_date_time_format);
var minutes = (endMillis - startMillis) / (1000 * 60);
if(minutes > 480) {
alert('An Activity may only last 8 hours. You must enter multiple activities if the time required exceeds 8 hours.');
g_form.setValue('work_end', '');
}
}
Obviously you will need to calculate the a year to then compare for your alert. Thinking out loud an onSubmit may be better because you can prevent the save if it exceeds a year.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2016 07:50 AM
Hi Mike,
Yes all is well. I marked your answer as the Correct Answer; however, I used the sample script that you provided but it doesn't seem to calculating. Can you take a look for me.
So I have a Begin Date and an End Date. The duration of those days should not exceed 365 days and I am only using the Date not the Date / Time.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var starts = g_form.getValue('priv_begin_date');
startMillis = getDateFromFormat(starts, g_user_date_time_format);
var ends = newValue;
endMillis = getDateFromFormat(ends, g_user_date_time_format);
var minutes = (endMillis - startMillis) / (1000 * 60);
if(minutes > 525600) {
alert('Duration between the begin and end dates cannot exceed 365 Days');
g_form.setValue('priv_end_date', '');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2016 09:36 AM
Karen, the getDateFromFormat() function is expecting date and time and with your variables being only a date, that is what is tripping it up. No big deal as we can easily turn your date into a date/time by adding ' 00:00:00' to each date. The following worked in my quick test:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var starts = g_form.getValue('priv_begin_date') + ' 00:00:00';
startMillis = getDateFromFormat(starts, g_user_date_time_format);
var ends = newValue + ' 00:00:00';
endMillis = getDateFromFormat(ends, g_user_date_time_format);
var minutes = (endMillis - startMillis) / (1000 * 60);
if(minutes > 525600) {
alert('Duration between the begin and end dates cannot exceed 365 Days');
g_form.setValue('priv_end_date', '');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2016 10:10 AM
It still accepted the request, not working
Begin Date = today's date
End Date = October xx, 2017
That is more than 365 Days. Not sure what the issue is ate this point.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2016 10:16 AM
Just to confirm, are these variables on the service catalog? Believe you had mentioned service catalog earlier.
I tested the code on catalog variables and also tested it on change with the work_start and work_end out of the box dates and it worked.
You can try putting a few alert()'s in there to see the values of the start and end calculations:
var starts = g_form.getValue('priv_begin_date') + ' 00:00:00';
startMillis = getDateFromFormat(starts, g_user_date_time_format);
alert('startMillis: ' + startMillis);
var ends = newValue + ' 00:00:00';
endMillis = getDateFromFormat(ends, g_user_date_time_format);
alert('endMillis: ' + endMillis);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2016 10:25 AM