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-20-2023 08:29 AM
@Community Alums
Please add one more condition to the UI policy.
End date is not empty.
Please mark my answer as correct based on Impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2023 12:26 AM
@Community Alums
you will require 2 onChange 1 for start and 1 for end
Either create 2 onChange or 1 onSubmit to validate
I am sharing onSubmit, you can use similar in onChange
function onSubmit() {
//Type appropriate comment here, and begin script below
g_form.hideErrorBox('start_date');
g_form.hideErrorBox('end_date');
if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
var start = new Date(g_form.getValue('start_date')).getTime();
var end = new Date(g_form.getValue('end_date')).getTime();
if(end < start){
var message = 'Please give valid start and end dates';
g_form.showErrorBox('start_date', message);
g_form.showErrorBox('end_date', message);
return false;
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2023 06:41 AM
Hi @Ankur Bawiskar ,
I tried the given code, but its not working, when I alert the dates, It shows me Invalid date. Could you please guide me on this.
Thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2023 08:00 AM
@Community Alums
please share your latest script
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2023 05:10 AM
Hi @Ankur Bawiskar ,
Below is my latest script:
function onSubmit() {
//Type appropriate comment here, and begin script below
if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
var start = new Date(g_form.getValue('start_date')).getTime();
var end = new Date(g_form.getValue('end_date')).getTime();
alert(start+""+end); //this gives me invalid date
if(end < start){
alert("true");
return false;
}
}
}