0

I am using the basic JQuery UI datepicker, I need the date range with check in and check out but I need to add date + x days to the check out input.

How can I add x days to $.datepicker.parseDate( dateFormat, element.value );

Here is the code:

$(function() {

    function getDate(element) {
        var date;
        try {
            date = $.datepicker.parseDate(dateFormat, element.value);
        } catch (error) {
            date = null;
        }

        return date;
    }
    var dateFormat = 'dd MM yy',
        from = $('#CheckIn')
        .datepicker({
            minDate: 0,
            dateFormat: 'dd MM yy',
            defaultDate: '+1w',
            changeMonth: false,
            numberOfMonths: 1
        })
        .on('change', function() {
            to.datepicker('option', 'minDate', getDate(this));
        }),
        to = $('#CheckOut').datepicker({
            dateFormat: 'dd MM yy',
            defaultDate: '+1w',
            changeMonth: true,
            numberOfMonths: 1
        })
        .on('change', function() {
            from.datepicker('option', 'maxDate', getDate(this));
        });
});

When I select a date from "Check In" I want "Check Out" to only enable date + x days from the From selected day.

Any help would be much appreciated.

Thanks in advance!

3 Answers 3

0

That Should work by modifying the onChange event of the #CheckIn datepicker:

$(function () {
  var dateFormat = 'dd MM yy',
      X = 2, // Number of days to add
      from = $('#CheckIn')
        .datepicker({
          minDate: 0,
          dateFormat: dateFormat,
          defaultDate: '+1w',
          changeMonth: false,
          numberOfMonths: 1
        })
        .on('change', function () {
          var checkInDate = getDate(this);
          if (checkInDate) {
            var checkOutMin = new Date(checkInDate);
            checkOutMin.setDate(checkOutMin.getDate() + X);

            to.datepicker('option', 'minDate', checkOutMin);
            to.datepicker('setDate', checkOutMin); 
          }
        }),
      to = $('#CheckOut').datepicker({
        dateFormat: dateFormat,
        defaultDate: '+1w',
        changeMonth: true,
        numberOfMonths: 1
      });

  function getDate(element) {
    var date;
    try {
      date = $.datepicker.parseDate(dateFormat, element.value);
    } catch (error) {
      date = null;
    }
    return date;
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you need CheckOut to be available as range from CheckIn to CheckIn + maxDays:

$(function () {
  var maxDays = 7;
  var dateFormat = 'dd MM yy',
    from = $('#CheckIn')
      .datepicker({
        minDate: 0,
        dateFormat: dateFormat,
        defaultDate: '+1w',
        changeMonth: false,
        numberOfMonths: 1
      })
      .on('change', function () {
        var checkInDate = getDate(this)
        if (checkInDate) {
          var checkOutMax = new Date(checkInDate)
          checkOutMax.setDate(checkInDate.getDate() + maxDays)

          to.datepicker('option', 'minDate', checkInDate)
          to.datepicker('option', 'maxDate', checkOutMax)
        }
      }),
    to = $('#CheckOut').datepicker({
      dateFormat: dateFormat,
      defaultDate: '+1w',
      changeMonth: true,
      numberOfMonths: 1
    })
      .on('change', function () {
        from.datepicker('option', 'maxDate', getDate(this))
      })

  function getDate (element) {
    var date
    try {
      date = $.datepicker.parseDate(dateFormat, element.value)
    } catch (error) {
      date = null
    }
    return date
  }
})

1 Comment

That worked a treat, thank you so much
0

With these changes, your code should work as expected:

  • Custom getDate() function: added interval as param for the date check as it needs to be returned to the CheckOut datepicker.
  • defaultDate and minDate settings: For ChekckIn: both where set to today's date (Now), assuming reservations should not start in the past. For CheckOut: dynamically calculated based on the selected CheckIn date plus the defined interval.
  • Event listener on CheckIn: changed from on to change.
  • Event listener on checkout removed: omitted, based on the assumption that rules only apply to setting the CheckOut based on CheckIn, and not the other way around.

$(function () {

    function getDate(element, interval) {
        var date;
        try {
            date = $.datepicker.parseDate(dateFormat, element.value);

            // Create instance of date from the datepicker element value and add the interval
            newDate = new Date(date);
            newDate.setDate(newDate.getDate() + parseInt(interval));

        } catch (error) {
            newDate = null;
        }

        // Return new date instead of date
        return newDate;
    }
    var dateFormat = 'dd MM yy',
        interval = 7,
        from = $('#CheckIn').datepicker({
            dateFormat: 'dd MM yy',
            minDate: 'Now',
            defaultDate: 'Now',     // Set both defaultDate and minDate to today's date, as it makes sense for CheckIn to start from today onward. Feel free to remove this if not needed.
            changeMonth: true,
            numberOfMonths: 1
        }).change(function () {
            to.datepicker('option', 'minDate', getDate(this, interval));
        }),
        to = $('#CheckOut').datepicker({
            dateFormat: 'dd MM yy',
            defaultDate: getDate(from, interval), // Set defaultDate based on CheckIn date
            minDate: getDate(from, interval),  // Also set minDate based on CheckIn date
            changeMonth: true,
            numberOfMonths: 1
        });

        // The checkout change listener was removed, based on the understanding that the rule applies exclusively to the CheckOut date.
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.2/jquery-ui.min.js"></script>
</head>
<body>
    <label for="CheckIn">CheckIn</label>
    <input type="text" name="CheckIn" id="CheckIn">

    <label for="CheckOut">CheckOut</label>
    <input type="text" name="CheckOut" id="CheckOut">

    <script src="script.js"></script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.