Search code examples
jqueryjquery-uidatedatepickerjquery-ui-datepicker

JQuery Datepicker: If selected date is today's date


I am able to determine if the selected date is in the past by using:

var due_date = $('#due_date').val();
if(new Date(due_date).getTime() <  new Date().getTime())
{
  //do stuff
}

^This works fine

I am using the following to determine if a selected date is today's date:

var due_date = $('#due_date').val();
var today = new Date().getTime();
if(new Date(due_date).getTime() == today)
{
    alert('ok');
}

But it's not hitting that alert. What is wrong with the above statement?


Solution

  • The string "30/03/2012" when used to create a date results in a Date object that represents midnight on the 30 March 2012. When you call new Date() it creates a Date object that represents the current time (including seconds and milliseconds).

    You'll need to set the hour, minute, second and millisecond properties of your Date object to 0 so that they represent the exact same time, using the setHours(), setMinutes(), etc functions.

    For more information about the Date object take a look at the MDN entry.