I have been reading obsessively in SO how to perform a date object comparison in Javascript. Got some really good ideas and pointers, but just cant seem to get any joy.
Here is the issue. I am using jquery datepicker to set a hidden value. If the hidden value changes, I would like to compare this date to today’s date, If they match, I would like to send an alert to the console.
Here is my script. No errors are reported in the chrome console; it just doesn’t seem to want to make that date comparison I am after.
var date2 = new Date();
$("select[name=sanctionDateStart_hidden]").change(function () {
if ($("select[name=sanctionDateStart_hidden]").getTime() == date2.getTime())
alert("yayayayaya");
});
Appreciate any insight anyone cares to share.
EDIT:
Here is what ended up resolving my issue, just posting this in case anyone else can use this. Thanks to post below to help me get going in the right direction.
var date2 = new Date();
$("#sanctionDateStart").datepicker({
altField: "#sanctionDateStart_hidden",
altFormat: "yy-mm-dd",
minDate: new Date()
}).datepicker("setDate", "{$data.sanctionDateStart}").change(function () {
$('#sanctionDateEnd').datepicker('option', 'minDate', $(this).datepicker('getDate'));
if ($("#sanctionDateStart").datepicker("getDate").getFullYear() == date2.getFullYear()
&& $("#sanctionDateStart").datepicker("getDate").getMonth() == date2.getMonth()
&& $("#sanctionDateStart").datepicker("getDate").getDate() == date2.getDate())
{
alert ('Todays Date');
}
});
Javascript Date is actually a time that includes the current date and also hour , minutes , seconds and milliseconds. So it would seldom match a date picked by a date picker
Use
onlyDateWithoutTimein your comparisons.Or do