0

I'm using jQuery DatePicker to ensure that a departure date is at least 1 day after an arrival date. I got the code to do this from this question.

However, I've just noticed that if I choose tomorrow (31st May 2013) as the arrival date, the departure date gets set to 10th January 2019! But if I pick an arrival date after tomorrow, it seems to work fine. Any ideas on what's going wrong with this? Thanks for any help.

Here's a JSFiddle with the code that shows the issue.

And here's the JS itself:

$(".datepicker_arrival").datepicker({
  dateFormat: 'dd/mm/yy',
  minDate: new Date(),
  onSelect: function(dateText, inst) {
    if($('.datepicker_departure').val() == '') {
      var current_date = $.datepicker.parseDate('dd/mm/yy', dateText);
      current_date.setDate(current_date.getDate()+1);
      $('.datepicker_departure').datepicker('setDate', current_date);
    }
  },
  onClose: function( selectedDate, test) {
     var  MyDateString = ('0' + (parseInt(test.selectedDay)+1)).slice(-2) + '/'
             + ('0' + (test.selectedMonth+1)).slice(-2) + '/'
             + test.selectedYear;
      $( ".datepicker_departure" ).datepicker( "option", "minDate", MyDateString);
  }
});

$(".datepicker_departure").datepicker({
  dateFormat: 'dd/mm/yy',
  minDate: new Date(),
  onClose: function( selectedDate ) {
    $( ".datepicker_arrival" ).datepicker( "option", "maxDate", selectedDate );
  }
});
Community
  • 1
  • 1
Stephen
  • 553
  • 3
  • 12
  • 23
  • 1
    It's the +1 on the selectedDay day that's causing it. That's the part that stops you from being able to select the same day in both parts. I'm looking back at the code. – MasNotsram May 31 '13 at 13:09
  • 1
    This should hopefully fix it: http://jsfiddle.net/9mSxk/3/ – MasNotsram May 31 '13 at 13:24
  • I appreciate you taking the time to look into this and find a fix. I've just tried the code and it works great, so thank you for this. :-) – Stephen May 31 '13 at 14:35
  • Also, feel free to add this as the official answer and I'll mark it as such. Thanks. – Stephen May 31 '13 at 14:36
  • Glad to hear it works. It's okay, I should have covered that base in the initial answer, I'm simply earning the right to keep the accepted answer to the initial question :) You may want to test the new fix across a range of browsers, just to be sure, as I didn't test in many. – MasNotsram May 31 '13 at 14:42
  • The version in the other question/answer seemed to work fine in most situations. This questions deals with an edge case bug that came to light after the event. So I think this is worthy of an answer in itself. Plus, I don't want to have an answerless question - so if you don't, I will, but I'd rather you added it as credit where credit's due. :) – Stephen Jun 01 '13 at 13:25
  • Okay, that's fair. I've posted an answer :) – MasNotsram Jun 03 '13 at 08:15
  • It looks like the updated version of jQueryUI handles this better. – Mike Cole Aug 15 '13 at 15:18

1 Answers1

1

Okay to summarize the solution in the comments (which was provided by me, I'm not just leeching an answer).

The error was caused by this piece of code:

var  MyDateString = ('0' + (parseInt(test.selectedDay)+1)).slice(-2) + '/'
             + ('0' + (test.selectedMonth+1)).slice(-2) + '/'
             + test.selectedYear;

which was not correctly adding a day to the date. When adding the day, the months were not taken into account, so any end of the month caused a date like so to be created:

32/05/2013

Of course there is no 32nd of the month.

Therefore, a suggested fix code can be found here:

jsfiddle.net/9mSxk/3

MasNotsram
  • 2,105
  • 18
  • 28