- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-15-2021 10:27 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2021 11:12 PM
I've provided a script include example above but here is a script to do the check just in the client side.
I have 2 fields, start_date and end_date. Both of type Date.
User's date format may be arbitrary. That is, it may be dd-MM-yyyy or yyyy/MM/dd, or something else as selected by the user.
2 client scripts are necessary. One onChange client script on start_date. Another onChange client script on end_date.
1. start_date client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
var endDate = g_form.getValue('end_date');
if (endDate.length < 1 || newValue.length < 1) { // if either of the field is empty, don't compare
return;
}
var startDate = new Date(getDateFromFormat(newValue, g_user_date_format)); // get start_date. convert from user's date format
endDate = new Date(getDateFromFormat(endDate, g_user_date_format)); // get end_date. convert from user's end format
g_form.hideFieldMsg('end_date'); // hide error message in end_date
if (startDate.getTime() > endDate.getTime()) { // compare dates
g_form.showFieldMsg('start_date', 'start date must be before end date.', 'error'); // show error message
}
} catch (err) {
alert(err.message);
}
}
2. end_date client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
var startDate = g_form.getValue('start_date');
if (startDate.length < 1 || newValue.length < 1) { // if either of the field is empty, don't compare
return;
}
var endDate = new Date(getDateFromFormat(newValue, g_user_date_format)); // get end_date. convert from user's end format
startDate = new Date(getDateFromFormat(startDate, g_user_date_format)); // get start_date. convert from user's date format
g_form.hideFieldMsg('start_date'); // hide error message on start_date if there is any
if (endDate.getTime() < startDate.getTime()) { // check if end_date is after start_date
g_form.showFieldMsg('end_date', 'end date must be after start date.', 'error'); // show error message
}
} catch (err) {
alert(err.message);
}
}
Execution result:
date format: dd/MM/yyyy
1. start_date = 10/12/2021
2. end_date = 09/12/2021
3. start_date = 08/12/2021

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-15-2021 10:52 PM
Can you describe your requirement in detail. If you want to compare date in Client script then you don't need Script Include. Are you comparing data field with current date in client script?
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-15-2021 11:07 PM
i want write script for start date is less than end date i know logic but how to write by using glideajax. for practice purpose
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2022 05:23 AM
If I have type date/time then what I have to write. Could you please help me?
My requirement is I have 2 date/time field as first day and last day both are date/time type field.
both can be same like current or future date should be allowed and first day should be less than last day or last day should be same as first day of greater than first day. could you help me on this please?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-15-2021 11:07 PM
Following will compare dates using client script and script include.
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('CompareDates');
ajax.addParam('sysparm_name', 'compareDates');
ajax.addParam('sysparm_start_date', g_form.getValue('start_date'));
ajax.addParam('sysparm_end_date', newValue);
ajax.getXMLAnswer(function(answer) {
alert(answer);
});
}
Script include:
var CompareDates = Class.create();
CompareDates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
compareDates: function() {
try {
var startDate = new GlideDateTime(this.getParameter('sysparm_start_date'));
var endDate = new GlideDateTime(this.getParameter('sysparm_end_date'));
return (startDate < endDate).toString();
} catch (err) {
return err.message;
}
},
type: 'CompareDates'
});