Date comparison issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi All,
Please provide you inputs for below issue.
Requirement:
In our custom table we have two fields, Start Date & End Date, every time if any new record is created, it should check with existing records and make sure to add start date before the start date of existing records.
I have written OnChange client script & Script includes.
but if I enter the date as 11-12-2025.//Dec 11th
Expected output is: Please select before: 12-11-2025
Actual output: no alert it is accepting.
In my RCA:
while comparing it is considering start date as mm-dd-yyyy.
In existing there are multiple records are created.
ex: Here End user changed their date format to dd-mm-yyyy.
| Fal ID | Start Date | End Date |
| 123 | 12-11-2025 | 31-12-2025 |
Sample code:
Output: 2025-11-12//Nov 12th
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
you are facing issue due to date format issue.
try this approach
1) in the onChange of Start Date, send the value user is entering and his/her date format and compare it with the last created record's End Date
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
return;
var ga = new GlideAjax('ValidateStartDate');
ga.addParam('sysparm_name', 'checkStartDateBeforeExisting');
ga.addParam('sysparm_start_date', newValue);
ga.addParam('sysparm_date_format', g_user_date_format);
ga.getXMLAnswer(function(response) {
if (response && response !== 'ok') {
alert(response); // Show message from Script Include if invalid
g_form.clearValue('u_start_date', ''); // Clear invalid date
}
});
}
Script Include: It should be client callable
var ValidateStartDate = Class.create();
ValidateStartDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkStartDateBeforeExisting: function() {
var inputDateStr = this.getParameter('sysparm_start_date');
var userDateFormat = this.getParameter('sysparm_date_format');
if (!inputDateStr)
return 'Start Date is required';
var gdtNew = new GlideDateTime();
// Parse user input in his/her date format
gdtNew.setDisplayValue(inputDateStr, userDateFormat + ' 00:00:00');
// Query your custom table (replace 'u_custom_table' with your table name) and fetch latest created record which has start date populated
var gr = new GlideRecord('u_custom_table');
gr.addNotNullQuery('u_start_date');
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if (gr.next()) {
var existingStartDate = gr.getValue('u_start_date');
var gdtExisting = new GlideDateTime();
gdtExisting.setDisplayValue(existingStartDate);
// Compare dates: newDate must be before existingStartDate
if (gdtNew.compareTo(gdtExisting) >= 0) {
// Format existing date for message in user format for clarity
var existingFormatted = existingDateOnly.getByFormat(userDateFormat);
return 'Please select Start Date before: ' + existingFormatted;
}
}
return 'ok'; // valid if no conflicts found
},
});
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader