Not able to compare date/time field

- 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;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2023 11:43 PM - edited ‎10-18-2023 11:45 PM
Hi @Community Alums ,
Please try the below code: OnChange script on Planned End Date field
Note : Pls use proper backend values in the below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var startDate = new Date(g_form.getValue('start_date')); //get the value of the start_date field using the g_form.getValue method
var checkEnd = new Date(newValue); // create a new JS date from the desired end_date field (using newValue parameter)
if (checkEnd <= startDate){ //Again - a simple JS Date object comparison
alert('End date must be after start date');
g_form.setValue('end_date','');
}
}
Thanks,
Danish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2023 06:37 AM
Hi @Danish Bhairag2 ,
I tried your code, but its not working. when I alert the dates to troubleshoot its saying invalid dates. Could you please help me on this.
Thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2023 12:22 AM
Hi @Community Alums ,
You can create a UI policy with the condition as below,
End date "Is more than" 0 Hours "before" start date and in the script execute if true you can write below code
Script:
function onCondition() {
alert('End Date should not be before Start Date');
g_form.clearValue('end_date_time');
}
Please check and Mark Helpful and Correct if it really helps you.
Regards,
Swathi Sarang

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2023 06:34 AM
Hi @swathisarang98 ,
I tried the method which you have mentioned, however, when the end date is empty still its saying end date should not be before start date. Could you please help me on this.
Thanks!!