views:

5441

answers:

3

What's the object type returned by Datepicker? Supposing I have the following:

$("#txtbox").datepicker({
   onClose: function(date){
          //something
       }
});

What is date? I'm interested in reading the date object from another Datepicker for comparison, something like:

   function(date){
       oDate = $("#oDP").datepicker("getDate");
       if(oDate == date)
          //do one
       else if(oDate > date)
          //do two
   }

However, this kind of comparison is not working. I'm guessing there is some sort of comparison method for Date object, but I don't know. I also tried comparing the String representation of the dates like oDate.toString() > date.toString() to no avail.

A: 

What is date?

it's the $("#txtbox") object

Javier
The JQuery documentation says that date is the Date object value of the Datepicker instance that is attached to the txtbox element. I have printed the value of date (alert(date)) and it is in fact a Date object.
Elliot Vargas
+3  A: 

A Date object is returned by the datePicker.

Your method for comparing dates is valid - from W3schools:

var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();

if (myDate>today)
{
    alert("Today is before 14th January 2010");
}

Are you getting a value in oDate from this line?

oDate = $("#oDP").datepicker("getDate");

Your comparison method seems valid - so I'm wondering if datePicker is successfully pulling a value from #oDP?

Edit - oDate confirmed to contain a valid date. This may be a very silly question, but have you confirmed that date contains a valid date? I'm wondering if there may be some issue with naming it the same as the keyword Date (Javascript keywords and reserved words). Perhaps try renaming it to tDate or the like in your function to be doubly-clear this isn't causing your problems.

ConroyP
My $("#oDP") is returning a Date. I have a alert(oDate) to verify.
Elliot Vargas
+3  A: 

I just downloaded the source from here and noticed (ex line 600) the author is using .getTime() to compare dates, have you tried that?

if (oDate.getTime() > date.getTime()) {
    ...
}

Also this is tangential but you mention you tried oDate.toString() while I noticed in the examples the author is using .asString()

Pat
Hi @Pat your link is now a 404 page.
Russell
Two years later the web has changed :-) I think I managed to track the new link and fixed it thanks for the heads up ...
Pat