jQuery validation to compare start and end date
Solution 1
If you have year, month and day values as numbers (they can be Type number or string), you can create a Date like:
var dStart = new Date(year, --month, day);
Note that for calendar month 1 the javascript month number is 0 (zero) as months are zero indexed, hence --month
.
and you can compare two dates directly:
if (dEnd < dStart) {
// end is before start
}
Solution 2
I think either of these links should answer your question in general:
validate end date equal to greater than start date
Validate that end date is greater than start date with jQuery
UPDATE:
This link describes exactly your question and gives a good solution for it
jQuery validate date field for seperate year, month, date
Solution 3
cant get simpler than this..
var date1 = Date.parse("2012-11-18");
var date2 = Date.parse("2011-11-18");
if (date1 > date2) {
alert ("Error!");
}
else{
alert("correct")
}
Sam
Updated on June 05, 2020Comments
-
Sam almost 4 years
I have two fields. One for person_start_date and another for person_end_date. Both has three separate fields for year, month, date. I would like to validate like Person end date > Person start date. Thanks in advance.
html.erb
<%= datetime_select :person_start_date %> <select id="person_start_date_1i" name="person[start_date(1i)]"> ... </select> <select id="person_start_date_2i" name="person[start_date(2i)]"> ... </select> <select id="person_start_date_3i" name="person[start_date(3i)]"> ... </select>
html.erb
<%= datetime_select :person_end_date %> <select id="person_end_date_1i" name="person[end_date(1i)]"> ... </select> <select id="person_end_date_2i" name="person[end_date(2i)]"> ... </select> <select id="person_end_date_3i" name="person[end_date(3i)]"> ... </select>