Home > Net >  How to set second litepicker startDate based on first litepicker startDate selection
How to set second litepicker startDate based on first litepicker startDate selection

Time:01-27

Hey my fellow Stackoverflowers, I am working with litepicker.js and ran into a snag that I am hoping one of you can help me figure out.

I have built out two separate litepickers and am successfully populating todays date on the first and tomorrows date in the second. (these are also successfully being accepted as the minDate for both)

What I have not been able to figure out is how to set the minDate for the second litepicker to one day after the selected startDate of the first litepicker.

You can see where I am at in this codepen https://codepen.io/DigitalDesigner/pen/oNGRWzV

HTML Code:

<form>
  <input id="start-date" > / 
  <input id="end-date" >
</form>

Javascript:

<script>
let current = new Date();
let tomorrow = new Date(current.getTime()   86400000); //   1 day in ms
tomorrow.toLocaleDateString();
// Add litepicker calendar to stay dates

const startpicker = new Litepicker({
    element: document.getElementById('start-date'),
    singleMode: true,
    allowRepick: true,
    autoRefresh: true,
    format: 'D MMMM YYYY',
    startDate: current,
    minDate: new Date(),
    numberOfMonths: 1,
    numberOfColumns: 1,
    tooltipText: {
        one: 'night',
        other: 'nights'
    },
    tooltipNumber: (totalDays) => {
        return totalDays - 1;
    },
    plugins: ['mobilefriendly']
});
const endpicker = new Litepicker({
    element: document.getElementById('end-date'),
    singleMode: true,
    allowRepick: true,
    autoRefresh: true,
    format: 'D MMMM YYYY',
    startDate: tomorrow,
    minDate: current,
    numberOfMonths: 1,
    numberOfColumns: 1,
    tooltipText: {
        one: 'night',
        other: 'nights'
    },
    tooltipNumber: (totalDays) => {
        return totalDays - 1;
    },
});
</script>

here is the litepicker site: https://litepicker.com/

How can I set the minDate for the second litepicker based on the first litepickers selected date?

Thanks in advance.

CodePudding user response:

add this to the code

startpicker.on('selected', (date1, date2) => {
    endpicker.setOptions({
        minDate: startpicker.getDate(),
        startDate: startpicker.getDate()
  });
});
  •  Tags:  
  • Related