JS时间选择器(当天已过去的时间不可选择)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <!-- <link href="__PUBLIC__/css/weixin/cart/orderindex.css" rel="stylesheet" type="text/css" /> -->
    <link rel="stylesheet" href="http://cdn.suoluomei.com/common/js2.0/HfPoints/v1/HfPoints.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css">
    <script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://at.alicdn.com/t/font_bar8ih9wy9rltyb9.js"></script>
    <script src="https://cdn.suoluomei.com/common/js2.0/region/region.js"></script>
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>

    <script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/swiper.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/city-picker.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,minimum=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>日期时间选择器</title>
    <style>
    html{
        touch-action: none;
    }
    </style>
</head>

<body>
    <input type="text" id="mentdate" placeholder="请选择日期">
    <div id="apptime">
        <input type="text" id="menttime" placeholder="请选择时间">
    </div>
    
    <input type="hidden" name="nowtime" id="nowtime">
</body>
<script>
    $(function () {
        var list = [
            '06:00-07:00', '07:00-08:00', '08:00-09:00', '09:00-10:00', '10:00-11:00', '11:00-12:00', '12:00-13:00', '13:00-14:00', '14:00-15:00', '15:00-16:00', '16:00-17:00', '17:00-18:00'
        ]
        //日历选择
        var datelist = []
        var minDate = new Date(new Date().setDate(new Date().getDate() - 1)).format("yyyy-MM-dd");
        var maxDate = new Date(new Date().setDate(new Date().getDate() + 2)).format("yyyy-MM-dd");
        var nowtime = new Date().format("yyyy-MM-dd hh:mm:ss");
        var expiretime = nowtime.substring(11, 16);
        console.log(maxDate)
        console.log(nowtime)
        console.log(expiretime)
        $("#nowtime").val(nowtime);
        $("#mentdate").calendar({
            title: '预约日期',
            minDate: minDate,
            maxDate: maxDate,
            dateFormat: 'yyyy-mm-dd',
            closeByOutsideClick: true,
            onChange: function (picker, values, displayValues) {

                if (values[0] == '') {
                    $.alert('请选择预约日期');
                }
                $("#mentdate").val(values)
                var today = $("#mentdate").val()
                console.log(today)
                // book_date=today
                var nowdate = $("#nowtime").val().substring(0, 10);
                var expiretime = $("#nowtime").val().substring(11, 16);
                // console.log(today)
                //时间选择

                //日历选择end
                //选择今天时
                if (today == nowdate) {
                    datelist = []
                    for (let i in list) {
                        if (expiretime < list[i].substring(0, 5)) {
                            datelist.push(list[i])
                            $('#apptime').html(`<input type="text" id="menttime" placeholder="请选择时间">`)
                            console.log("1111111111===>", datelist)
                            $("#menttime").picker({
                                title: "请选择时间",
                                cols: [
                                    {
                                        textAlign: 'center',
                                        values: datelist
                                    }
                                ],
                            })
                        } else {
                            $('#apptime').html(`<input type="text" id="menttime" placeholder="请选择时间">`)
                            datelist = []
                            $("#menttime").picker({
                                title: "请选择时间",
                                cols: [
                                    {
                                        textAlign: 'center',
                                        values: datelist
                                    }
                                ],
                            })
                        }
                    }

                }
                //选择不是今天时
                else if (today != nowdate) {
                    datelist = []
                    $('#apptime').html(`<input type="text" id="menttime" placeholder="请选择时间">`)
                    console.log("1111111", list[0])
                    for (let i in list) {
                        datelist.push(list[i])
                    }


                    console.log("---------------", datelist)
                    $("#menttime").picker({
                        title: "请选择时间",
                        cols: [
                            {
                                textAlign: 'center',
                                values: datelist
                            }
                        ],
                    })
                }
            }

        })


    })
    //格式化日期
    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,                 //月份 
            "d+": this.getDate(),                    //日 
            "h+": this.getHours(),                   //小时 
            "m+": this.getMinutes(),                 //分 
            "s+": this.getSeconds(),                 //秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }
</script>

</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值