前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Datepicker日期选择器插件

Datepicker日期选择器插件

作者头像
从入门到进错门
发布2018-08-21 16:39:31
3.7K0
发布2018-08-21 16:39:31
举报

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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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/

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1pk0f70ievu1e

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年08月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Datepicker日期选择器插件
    • 效果预览:
      • html:
        • js:
          • github地址:https://github.com/usecodelee/javascript–Datepicker
            • 官方文档:https://bootstrap-datepicker.readthedocs.io/en/stable/
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档