Kranthi
Kranthi

Reputation: 1417

jQuery string Comparison?

I'm comparing two dates and my code goes like this.

jQuery('.newAppointment a.ui-state-default').click(function() {
  var date = jQuery(this).parent().attr('title');
 var d = jQuery.datepicker.parseDate('dd/mm/yy',date.toString());
  alert(d);
  var today = new Date;
  var t = jQuery.datepicker.parseDate('dd/mm/yy',today.toString() );
  alert(t)
  if(t > d){
  url = "/users/" + user_id + "/events/new?type=Surgery"+"&day=" + escape(date);;
  window.location = url;
  }else{
    alert("you cannot add appointment to past dates");
  }
});

but am getting error in firebug. uncaught exception: Missing number at position 0 can anyone tell me where I'm doing wrong.

Upvotes: 0

Views: 2409

Answers (2)

mu is too short
mu is too short

Reputation: 434675

From the fine manual:

parseDate(format, value, settings)
[...]
A number of exceptions may be thrown:

  • 'Missing number at position nn' if format indicated a numeric value that is not then found

So your error is coming from jQuery-UI. The format you get from date.toString() depends on the browser and the locale, there's no reason to expect it always be dd/mm/yy and in your case, it isn't.

Your date is already a string and in a known format (presumably dd/mm/yy) so you should be able to do this:

var d = jQuery.datepicker.parseDate('dd/mm/yy', date);

to get a Date. Then you can get today with just:

var today = new Date;

and compare them directly:

if(today > d)

If you want to throw away the hours, minutes, and seconds then:

var now   = new Date;
// Or set milliseconds, seconds, minutes, and hours to zero.
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
if(today > d)

Upvotes: 3

Tejasva Dhyani
Tejasva Dhyani

Reputation: 1362

may be there is some problem with parseDate() You can compare the dates just by using just

if (today > date){...}

Upvotes: 0

Related Questions