mymth / vanillajs-datepicker

A vanilla JavaScript remake of bootstrap-datepicker for Bulma and other CSS frameworks
MIT License
744 stars 155 forks source link

DateRangePicker limit the number of days between #35

Open mrcycling opened 4 years ago

mrcycling commented 4 years ago

Is there a way to limit the number of days between the two inputs with the DateRangePicker? Such as limiting the second date to be less than 30 days after first date selected or more than 90 days or even equal to 7 days.

We could use the changeDate API, with a little math and popup an alert if they exceeded the suggested time frame, but it would be easier if the calendar only offered dates within the specified period.

imranhsayed commented 2 years ago

@mrcycling Try using this:

    getFormattedDate( date ) {
        const year = date.getFullYear();

        let month = ( 1 + date.getMonth() ).toString();
        month = month.length > 1 ? month : '0' + month;

        let day = date.getDate().toString();
        day = day.length > 1 ? day : '0' + day;

        return month + '-' + day + '-' + year;
    }
        const dateRangePicker = new DateRangePicker( element, {
            defaultViewDate: '10-10-2022', // mm-dd-yyyy
            beforeShowDay: ( date ) => {
                const allowedDates = [ '10-10-2022', '10-11-2022' ]; // mm-dd-yyyy ( Allowed date range )
                const theDate = getFormattedDate( date );
                return allowedDates.includes( theDate );
            },
        } );

Ref: https://mymth.github.io/vanillajs-datepicker/#/options?id=beforeshowday