Datepicker日期选择器插件

Datepicker日期选择器插件

这个插件还是比较简单的,而且样式也比较漂亮,可以自定义选择年月日、年月日时分、年月、时间段选择等等。

效果预览:

这个插件是基于jQuery和bootstrap的。因此我们需要引入的文件有:
  1. bootstrap.min.cssbootstrap.css
  2. bootstrap-datepicker.min.cssbootstrap-datepicker.min.css
  3. jquery.min.jsjquery.js
  4. bootstrap-datepicker.min.jsbootstrap-datepicker.js
  5. bootstrap-datepicker.zh-CN.js

其中第五个表示将插件换成中文显示

具体使用方法见代码:

html:

<div class="container">
    <h1 class="text-center text-info">Datepicker日期选择器插件</h1>
    <!-- 年月日 -->
    <div class="form-group has-success">
        <label class="control-label" for="ymd">年月日
        <div class="input-group">
            <span class="input-group-addon glyphicon glyphicon-time"></span>
            <input type="text" class="form-control" id="ymd" aria-describedby="inputGroupSuccess1Status">
        </div>
    </label>
    </div>

    <!-- 年月日时分 -->
    <div class="form-group has-warning">
        <label class="control-label" for="ymdhm">年月日时分
        <div class="input-group">
            <span class="input-group-addon glyphicon glyphicon-time"></span>
            <input type="text" class="form-control" id="ymdhm" aria-describedby="inputGroupSuccess1Status">
        </div>
    </label>
    </div>
    <table class="table">
        <thead>
            <tr>
                <th>
                    <div class="form-group has-error">
                        <label class="control-label" for="start">开始时间
                        <div class="input-group">
                            <span class="input-group-addon glyphicon glyphicon-time"></span>
                            <input type="text" class="form-control" id="start">
                        </div>
                    </label>
                    </div>
                </th>
                <th>
                    <div class="form-group has-error">
                        <label class="control-label" for="end">结束时间
                        <div class="input-group">
                            <span class="input-group-addon glyphicon glyphicon-time"></span>
                            <input type="text" class="form-control" id="end">
                        </div>
                    </label>
                    </div>
                </th>
            </tr>
        </thead>
    </table>
</div>

js:

<script>
        $('#ymd').fdatepicker({
            format: 'yyyy-mm-dd',
            language: "zh-CN"
        });
        $('#ymdhm').fdatepicker({
            language: "zh-CN",
            format: 'yyyy-mm-dd hh:ii',
            pickTime: true
        });


        var nowTemp = new Date(0, 0, 0, 0, 0, 0);
        var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
        var checkin = $('#start').fdatepicker({
            language: "zh-CN",
            format: 'yyyy-mm-dd',
            onRender: function(date) {
                return date.valueOf() < now.valueOf() ? 'disabled' : '';
            }
        }).on('changeDate', function(ev) {
            if (ev.date.valueOf() > checkout.date.valueOf()) {
                var newDate = new Date(ev.date)
                newDate.setDate(newDate.getDate() + 1);
                checkout.update(newDate);
            }
            checkin.hide();
            $('#end')[0].focus();
        }).data('datepicker');
        var checkout = $('#end').fdatepicker({
            language: "zh-CN",
            format: 'yyyy-mm-dd',
            onRender: function(date) {
                return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
            }
        }).on('changeDate', function(ev) {
            checkout.hide();
        }).data('datepicker');
</script>

github地址:https://github.com/usecodelee/javascript–Datepicker

官方文档:https://bootstrap-datepicker.readthedocs.io/en/stable/

猜你喜欢

转载自blog.csdn.net/caomage/article/details/81480515