Even though this person's issue was resolved, I still want to answer the question in the title, "How to compare two datepicker dates using jQuery"
I prefer getting the timestamps of the dates and then just comparing them since they're numbers, that way I don't have to mess around with comparing months, years, leap years, etc. I just want to see which date is chronologically first.
To do that, you can use the getTime() function on the datepicker object like this: $from = $("#fromDate").datepicker('getDate').getTime();
This function returns the number of milliseconds since January 1st, 1970, 00:00:00.000 GMT. If the number is smaller, that means it's closer to that date in the past. If it's bigger, then it's farther away (in the future).
See below for a working snippet.
// Init datepickers
$('.datepicker').datepicker();
$('button').on('click', compareDates);
function compareDates() {
var $from = $("#fromDate"),
$to = $("#toDate");
if ($from.val() && $to.val()) {
var from = $from.datepicker('getDate').getTime(),
to = $to.datepicker('getDate').getTime(),
response = 'These dates are the same, <strong><em>invalid!</em><strong>';
if (from > to) {
response = 'To happens first, <strong><em>invalid!</em><strong>';
} else if (from < to) {
response = 'From happens first, valid!';
}
$('#output').html(response);
}
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<p>From Date: <input type="text" id="fromDate" class="datepicker"></p>
<p>To Date: <input type="text" id="toDate" class="datepicker"></p>
<p><button>Compare Dates</button></p>
<p id="output">Enter Dates and then click the button to compare them</p>