views:

95

answers:

1

I just got IE6 sprung on me for a project that is going out into the wild soon, which means it's time to go back and comb through all of the CSS and JS. I've gotten hung up on the date object, however:

$.validator.addMethod("dateRange", function() {
  var today = new Date();
  var event_date_raw = $('#event_date').val();
  var event_date_parts = event_date_raw.split("-");
  var event_date = new Date( event_date_parts[2]+","+event_date_parts[1]+","+event_date_parts[0] );
  if( event_date.getTime() >= today.getTime() )
   return true;
  return false;
 }, "Please specify a correct date:");

event_date.getTime() returns "NaN" in IE6 so the validation fails. The event_date_raw is in the YYYY-MM-DD format, which date doesn't seem to mind in every other browser...

Thoughts?

+2  A: 

According to MSDN you can also input a date value to a new Date object using numeric values. What happens if you try

var event_date = new Date( event_date_parts[0], event_date_parts[1] - 1, event_date_parts[2] );

Note that you have to pass the month number as a value between 0 and 11. In the example I decremented your month number, because I supposed that the input range is between 1 and 12.

Marcel Korpel
Worked like a charm. Thanks much. :) Also, here's a link to a similar question w/ a nice solution: http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok
J. LaRosee
@J.LaRosee: Good answer, indeed. If you feed `Date()` a string, like you did in your question, it's up to the browser to parse it. If you follow the [ECMA spec](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf) and adhere to the standard defined in 15.9.2.1 (Date Constructor Called as a Function), it's way more likely it will function cross-browser (but not a guarantee, alas).
Marcel Korpel