
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 05:54 PM
I am working on functionality as below
I have two fields on catalog item includes – start date and end date. We do not want users to select end date not more than 6 months from start date.
For example: start date = 07/22/2020
If End date = 1/22/2021 (technically difference is more than 180 days because of few months has 31 days ).
Let us say if end user selects 1/23/2021 as end date, then I should alert them saying not to select as it is more than 6 months calendar days.
I would really appreciate your help.
Thanks
Lakshman
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2020 08:58 AM
Below worked for me:
1) If start date is 22nd July then adding 6months to that is 22nd Jan
2) Now it depends on how 6 months you consider since some months are having 30 and 31 days.
3) I would request to check with business for exact days and that would be better.
a) So if user gives 23rd Jan then it gives alert and clears as it is not valid
b) If user gives 22nd Jan then it is ok
Client Script:
Note: Ensure you give valid variable names for start and end dates
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateTimeAjax');
ga.addParam('sysparm_name', "compareDates");
ga.addParam('sysparm_start', g_form.getValue('start_date')); // start variable
ga.addParam('sysparm_end', g_form.getValue('end_date')); // end variable
ga.getXMLAnswer(function(answer){
if(answer != ''){
alert('End date should be within 6 months from start date');
g_form.clearValue('end');
}
});
//Type appropriate comment here, and begin script below
}
Script Include: It should be client callable
var DateTimeAjax = Class.create();
DateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
compareDates: function(){
var start = new GlideDateTime(this.getParameter('sysparm_start'));
var end = new GlideDateTime(this.getParameter('sysparm_end'));
start.addMonthsUTC(6);
if(end.getNumericValue() > start.getNumericValue()){
return 'End Date should be withing 6 months from start';
}
return '';
},
type: 'DateTimeAjax'
});
Output:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2020 06:58 AM
Hello Lakshman,
Yes correct so you can replace
MaxStartDate.setDate(MaxStartDate.getDate() + 180); this line of code with
MaxStartDate.setMonth(MaxStartDate.getMonth() + 6);
you will get exact date you are looking for.
regards,
Arjun Shinde

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2020 08:30 AM
Arjun,
here is what I have tried exactly as you mentioned but still not working.
var datesplit = (newValue + '').split('-');
alert(datesplit); // output when I select 01/23/2021 == 2021,01,23
var date = new Date(datesplit[2], datesplit[0] - 1, datesplit[1]);
alert("date : "+date); // output == date : Tue May 01 2091 00:00:00 GMT-0400 (Eastern Daylight Time) ---> which is 2091 (failed case), how can it be 2091 after spliting
var MaxStartDate = new Date();
MaxStartDate.setMonth(MaxStartDate.getMonth() + 6);
if (date > MaxStartDate) {
g_form.showFieldMsg('end_date', 'End Date should not be more than 6 months from start date', 'error');
g_form.clearValue('end_date');
} else {
g_form.hideFieldMsg('end_date');
}
}
Note: I am writing this script in HR scoped application, not sure if this method works in scoped app.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2020 09:22 AM
Hello Lakshman,
there is a small change in code.
instead of this : var date = new Date(datesplit[2], datesplit[0] - 1, datesplit[1]);
please use below code.
Please change parameter sequence.
var date= new Date(datesplit[0],datesplit[1]-1,datesplit[2]);
Mark Correct and Helpful if it helps .
Best Regards,
Arjun Shinde
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 10:56 PM
Hi Lakshman,
Addition to Namrta, check this link
https://www.servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/
If my answer helped you in any way, mark answer as helpful and correct.
Thanks and regards,
Megha

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2020 11:04 PM
Hi Lakshman,
In addition to above, I will suggest you to try no code solution which will be best to keep system performance.
Create catalog ui policy as below and change variable names as per requirement.
Mark correct and helpful based on impact.
Thanks,
Dhananjay.