$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - How to grab selected dates from flatpickr - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to grab selected dates from flatpickr - Stack Overflow

matteradmin19PV0评论

I have a range date picker at the moment.

<input id="rangeDatepicker2" class="g-font-size-12 g-font-size-default--md" type="text" data-rp-wrapper="#rangePickerWrapper2" data-rp-type="range" data-rp-date-format="d M Y" data-rp-default-date='["01 Jan 2016", "31 Dec 2017"]'>

I would like to reuse the dates but I am unable to select them for all scenarios. I tried the code below which works great if the date selected are in the same month. But my issue is this bit of code doesn't work if the dates span multiple months.

$("#rangeDatepicker2").change(function () {
    var dates = $('.selected');
    if (dates.length == 2) {
        var start = dates[0].dateObj;
        var end = dates[1].dateObj;

        //interact with selected dates here
    }
});

May I ask how do I properly grab selected date range of a flatpickr.

I have a range date picker at the moment.

<input id="rangeDatepicker2" class="g-font-size-12 g-font-size-default--md" type="text" data-rp-wrapper="#rangePickerWrapper2" data-rp-type="range" data-rp-date-format="d M Y" data-rp-default-date='["01 Jan 2016", "31 Dec 2017"]'>

I would like to reuse the dates but I am unable to select them for all scenarios. I tried the code below which works great if the date selected are in the same month. But my issue is this bit of code doesn't work if the dates span multiple months.

$("#rangeDatepicker2").change(function () {
    var dates = $('.selected');
    if (dates.length == 2) {
        var start = dates[0].dateObj;
        var end = dates[1].dateObj;

        //interact with selected dates here
    }
});

May I ask how do I properly grab selected date range of a flatpickr.

Share Improve this question asked Nov 11, 2019 at 18:45 MasterMaster 2,1633 gold badges36 silver badges93 bronze badges 1
  • Can you add your initialization code? Where you make your "#rangeDatepicker2" a flatpickr – Jimmy Commented Nov 11, 2019 at 20:36
Add a ment  | 

1 Answer 1

Reset to default 2

Many input plugins actually pass the values in a onChange handler. The values are available there.

Solution

Pass down an onChange handler when initializing flatpickr:

$("#rangeDatepicker2").flatpickr({
    mode: 'range',
    onChange: function(dates) {
        if (dates.length == 2) {
            var start = dates[0];
            var end = dates[1];

            // interact with selected dates here
        }
    }
})

Demo

onChange documentation: https://flatpickr.js/events/#hooks

Post a comment

comment list (0)

  1. No comments so far