jquery 我使用Flatpicker我想禁用过去的时间这是我目前有

fdbelqdn  于 2023-10-17  发布在  jQuery
关注(0)|答案(2)|浏览(120)
var timewsd = document.getElementById("current_datetime").value ;
            var currentdate = "";



            let roundDate = (date = new Date(timewsd), roundToMinutes = 60) => {
                const ms = 1000 * 60 * roundToMinutes;
                return new Date(Math.ceil(date.getTime() / ms) * ms);
            };

            var current_time = new Date(timewsd).getHours()+":"+new Date(timewsd).getMinutes();

            const date = new Date(timewsd);

            // if(current_time > '16:30'){
            //     currentdate =  setDate(timewsd.getDate() + 1);
            // }else{
            //     currentdate = document.getElementById("current_datetime").value;
            // }


            $("#kt_datepicker_2").flatpickr({
                minDate: "today",
                minuteIncrement: 1,
                enableTime: true,
                minTime :current_time
            });

但是,当选择未来日期时,时间也会限制到未来日期的当前时间。
https://flatpickr.js.org/examples/

eiee3dmh

eiee3dmh1#

不要使用minDate: "today",而是将其替换为minDate: new Date(),并删除minTime,您将获得所需的输出。
下面,我提供了一个活生生的例子供大家参考。

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
</head>
<body>
    <h1>Select a Date and Time</h1>
    <input type="text" id="datepicker" placeholder="Select date and time">
    
    <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
    <script>
        flatpickr("#datepicker", {
            enableTime: true,
            minuteIncrement: 1,
            minDate: new Date()
        })
    </script>
</body>
</html>
dvtswwa3

dvtswwa32#

如果你想禁用过去的时间过去的日期,然后按照以下代码:

<!DOCTYPE html>
<html>

<head>
    <!-- flatpicker CSS CDN  -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
</head>

<body>

    <h4>Select a Date and Time</h4>
    <input type="text" id="datepicker" placeholder="Select date and time">

    <!-- flatpicker JS CDN -->
    <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
    <script>
        const now = new Date();   // get current date and time
        var currentHour = now.getHours();   // get current hours
        var currentMinute = now.getMinutes();   // get current minutes
        flatpickr("#datepicker", {
            enableTime: true,
            minuteIncrement: 1,
            minDate: new Date(),
            minTime: `${currentHour}:${currentMinute}`,
        });
    </script>
</body>

</html>

如果您想仅禁用日期,请从flatpicker配置中删除minTime元素。

相关问题