Not able to compare date/time field
Community Alums
Not applicable
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2023 11:34 PM
Hi All,
I have to field start date and end date. I want to put validation so that end date should not be before start date. I am able to achieve this but it is getting failed in one case.
case:
Start date :31/10/2023
End date: 2/11/2023
In above case, code consider end date as less than start date.
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateTimeFunc');
ga.addParam('sysparm_name', 'compareDates');
ga.addParam('start_date', g_form.getValue('date_time_from'));
ga.addParam('end_date', g_form.getValue('date_time_to'));
ga.getXML(getEndDate);
}
function getEndDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer=='true')
{
alert("End date should be after start date");
  //g_form.clearValue('date_time_to');
}
}
Script includes
compareDates: function() {
var chg_start_date = this.getParameter('start_date');
var chg_end_date = this.getParameter('end_date');
if (chg_start_date > chg_end_date)
{
return true;
}
return false;
},
10 REPLIES 10
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2023 07:36 AM
@Community Alums
what came in alert for both the dates?
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader