Bad Programmer
Bad Programmer

Reputation: 3752

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?

Upvotes: 6

Views: 23044

Answers (5)

josepedro
josepedro

Reputation: 1

also you can do it like this:

$( function() {
      $( "#datepicker" ).datepicker(
        { dateFormat: "dd/mm/yy",
        minDate: 0, 
        //maxDate: "+12M +10D"   
        }
      );
    } );

with minDate: 0, you are able to display the current days ahead,

Upvotes: -1

aztechy
aztechy

Reputation: 640

The datepicker object has a getDate method you can use that returns a date value to compare it to a new date(). You do have to some further massaging on the new date, but this should get you what you want.

JsFiddle Example

HTML:

Date: <input type="text" id="thedate"/>
<div id="checkDate">Check Date</div>

JS:

$('#thedate').datepicker();

$('#checkDate').bind('click', function() {
  var selectedDate = $('#thedate').datepicker('getDate');
  var today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  if (Date.parse(today) == Date.parse(selectedDate)) {
    alert('today!');
  } else {
    alert('not today');
  }
});

Upvotes: 12

j08691
j08691

Reputation: 207919

You need to make sure you're comparing apples to apples. Here's an easy way to check:

jQuery:

$('#dp').datepicker({
    onSelect: function(dateText) {
        var today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()).getTime();
        var selected = new Date(dateText).getTime();
        if (today > selected) alert('prior to today');
        else if (today < selected) alert('after today');
        else alert('today');
    }
});​

jsFiddle example.

Upvotes: 2

Anthony Grist
Anthony Grist

Reputation: 38345

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.

Upvotes: 2

dennmat
dennmat

Reputation: 2718

getTime() returns milliseconds, instead compare the day month and year, which I believe a normal date comparison does(maybe wrong). See: http://www.w3schools.com/js/js_obj_date.asp

Upvotes: 0

Related Questions