bootstrap-datetimepicker.js插件

Introduction

Simple date/time picker component based on the work of Stefan Petre, with contributions taken fromAndrew Rowls and jdewit.

Demo

Default behavior in pt-BR, picks date/time with fast masked input typing (need only to type the numbers, the static part of the mask is inserted automatically if missing) or via the popup widget, which supports year, month, day, hour and minute views:
 

Code:

<div class="well">
  <div id="datetimepicker1" class="input-append date">
    <input data-format="dd/MM/yyyy hh:mm:ss" type="text"></input>
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
</div>
<script type="text/javascript">
  $(function() {
    $('#datetimepicker1').datetimepicker({
      language: 'pt-BR'
    });
  });
</script>
Similar to above example, but in US date/hour format:
 

Code:

<div class="well">
  <div id="datetimepicker2" class="input-append">
    <input data-format="MM/dd/yyyy HH:mm:ss PP" type="text"></input>
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
</div>
<script type="text/javascript">
  $(function() {
    $('#datetimepicker2').datetimepicker({
      language: 'en',
      pick12HourFormat: true
    });
  });
</script>
Disables date picker:
 

Code:

<div class="well">
  <div id="datetimepicker3" class="input-append">
    <input data-format="hh:mm:ss" type="text"></input>
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
</div>
<script type="text/javascript">
  $(function() {
    $('#datetimepicker3').datetimepicker({
      pickDate: false
    });
  });
</script>
Disables time picker:
 

Code:

<div class="well">
  <div id="datetimepicker4" class="input-append">
    <input data-format="yyyy-MM-dd" type="text"></input>
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
</div>
<script type="text/javascript">
  $(function() {
    $('#datetimepicker4').datetimepicker({
      pickTime: false
    });
  });
</script>

API

The widget class provides 4 methods to manipulate dates: ‘getDate’/’setDate’ for working with UTC and ‘getLocalDate’/’setLocalDate’ for working with local dates:

// Considering you are on a GMT-3 timezone and the input contains '2000-01-17 10:00'
var localDate = picker.getLocalDate(); // localDate === 2000-01-17 07:00
var utcDate = picker.getDate(); // utcDate === 2000-01-17 10:00
//
picker.setLocalDate(new Date(1998, 10, 11, 4, 30)); // input === 1998-10-11 07:30
picker.setDate(new Date(Date.UTC(1998, 10, 11, 4, 30))); // input === 1998-10-11 04:30

The date value can be unset by passing ‘null’ to any of the ‘set’ methods or by erasing the input:

var picker = $('#datetimepicker'l).data('datetimepicker');
picker.setLocalDate(null);
// or
picker.setDate(null);
// or
input.val('');
input.change();

The only event exposed is ‘changeDate’, which will expose ‘date’ and ‘localDate’ properties on the event object:

el.on('changeDate', function(e) {
  console.log(e.date.toString());
  console.log(e.localDate.toString());
});

Options

These are the default options for initializing the widget:

$.fn.datetimepicker.defaults = {
  maskInput: true,           // disables the text input mask
  pickDate: true,            // disables the date picker
  pickTime: true,            // disables de time picker
  pick12HourFormat: false,   // enables the 12-hour format time picker
  pickSeconds: true,         // disables seconds in the time picker
  startDate: -Infinity,      // set a minimum date
  endDate: Infinity          // set a maximum date
};

Examples of using these options can be seen in the file test/specs.coffee.

Complete sample markup

Copy and paste the following code in a file(eg: test.html) and it should produce a widget similar to the first demo:

<!DOCTYPE HTML>
<html>
  <head>
    <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" media="screen"
     href="http://tarruda.github.com/bootstrap-datetimepicker/assets/css/bootstrap-datetimepicker.min.css">
  </head>
  <body>
    <div id="datetimepicker" class="input-append date">
      <input type="text"></input>
      <span class="add-on">
        <i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
      </span>
    </div>
    <script type="text/javascript"
     src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script> 
    <script type="text/javascript"
     src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js">
    </script>
    <script type="text/javascript"
     src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.min.js">
    </script>
    <script type="text/javascript"
     src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.pt-BR.js">
    </script>
    <script type="text/javascript">
      $('#datetimepicker').datetimepicker({
        format: 'dd/MM/yyyy hh:mm:ss',
        language: 'pt-BR'
      });
    </script>
  </body>
<html>

Development

If you want to contribute, please follow the settings in the .lvimrc file (2 space indentation).

To report bugs, ideally an automated test that reproduces the bug should be created in the ‘test/issues.coffee’ file(following the conventions of the tests already defined there) and submitted with a pull request.

To compile/minify you need to have make, node.js and npm on your $PATH

$ git clone git://github.com/tarruda/bootstrap-datetimepicker.git
$ cd bootstrap-datetimepicker
$ make deps
$ make build

To run the automated tests:

$ make test

It should download all dependencies needed for testing(phantomjs, jquery…)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: bootstrap-datetimepicker 是一个强大的日期时间选择插件,它提供了双时间选择的功能。使用 bootstrap-datetimepicker,你可以在网页中方便地选择起始时间和结束时间。 要实现双时间选择,首先需要在 HTML 文件中引入 bootstrap-datetimepicker 的相关文件。然后,在需要显示双时间选择的输入框上添加对应的 class,比如"datetimepicker"。 接下来,在 JavaScript 中使用 $("#输入框的ID").datetimepicker() 来初始化双时间选择插件。你可以在初始化的时候设置一些参数,比如设置可选时间范围,或者指定时间格式等。 当你点击输入框时,就会弹出一个日期时间选择器。你可以使用上下箭头来选择日期和时间,也可以直接在输入框中手动输入。当你选择好起始时间和结束时间后,可以点击确定按钮来关闭选择器,并将选择的时间显示在输入框中。 双时间选择的输入框中会同时显示起始时间和结束时间,以指定时间范围。你也可以在选择器的界面上看到一个日历,以帮助你选择起始日期和结束日期。 总的来说,bootstrap-datetimepicker 是一个方便易用的日期时间选择插件,它可以帮助你实现双时间选择功能。无论是建立预约系统,还是进行时间范围查询,它都是一个非常有用的工具。 ### 回答2: bootstrap-datetimepicker是一个基于Bootstrap框架的日期时间选择器插件。它提供了一种简单易用的方式,让用户可以方便地选择日期和时间。 双时间选择指的是用户可以选择一个起始时间和一个结束时间。这种功能在一些需要时间段选择的场景中非常常见,比如航班预订、酒店预订等等。 使用bootstrap-datetimepicker实现双时间选择非常简单。首先,我们需要引入相关的库文件和样式表。然后,在HTML页面中创建两个输入框,分别用于显示起始时间和结束时间。接着,使用jQuery选择器找到这两个输入框,并调用datetimepicker()方法初始化它们。 具体的代码如下所示: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>双时间选择</title> <link rel="stylesheet" href="bootstrap.min.css"> <script src="jquery.min.js"></script> <script src="bootstrap.min.js"></script> <script src="moment.min.js"></script> <script src="bootstrap-datetimepicker.min.js"></script> </head> <body> <div class="container"> <h2>双时间选择</h2> <div class="form-group"> <label for="start-time">起始时间:</label> <input type="text" class="form-control" id="start-time"> </div> <div class="form-group"> <label for="end-time">结束时间:</label> <input type="text" class="form-control" id="end-time"> </div> </div> <script> $(function() { $('#start-time').datetimepicker(); $('#end-time').datetimepicker(); }); </script> </body> </html> ``` 通过上述代码,我们可以在页面上看到两个日期时间选择框。当用户点击输入框时,会弹出一个日期时间选择器,用户可以通过它来选择日期和时间。用户选择完毕后,选择器会自动将日期时间填充到相应的输入框中。 这样,我们就实现了使用bootstrap-datetimepicker进行双时间选择的效果。当然,我们还可以根据具体需求,进一步自定义日期时间选择的样式和功能。 ### 回答3: bootstrap-datetimepicker是一个基于Bootstrap框架的日期时间选择器插件。它允许用户同时选择起始时间和结束时间。 使用bootstrap-datetimepicker双时间选择,首先需要在HTML文件中引入相关的CSS和JavaScript文件。然后,通过指定相应的class或ID来创建两个日期时间选择器。 在HTML代码中,我们可以创建两个输入框和两个按钮来分别表示起始时间和结束时间。通过为输入框添加相应的class或ID,即可初始化日期时间选择器。例如: ``` <input type="text" class="datetimepicker-start" /> <input type="text" class="datetimepicker-end" /> <script type="text/javascript"> $(function () { $('.datetimepicker-start').datetimepicker(); $('.datetimepicker-end').datetimepicker(); }); </script> ``` 上述代码中,`datetimepicker-start`和`datetimepicker-end`分别为起始时间和结束时间的class。通过调用`.datetimepicker()`函数,即可初始化日期时间选择器。 初始化后,用户即可在输入框中点击选择日期和时间。同时,可以根据需要自定义日期时间格式、起始日期、最小/最大日期等选项。 此外,bootstrap-datetimepicker还提供了一些事件,可以让我们在选择日期时间后执行相应的操作,例如更新其他页面元素的内容。 总的来说,bootstrap-datetimepicker双时间选择功能简单易用,可以满足大部分日期时间选择的需求。通过添加相应的class或ID,并调用相关函数,即可在网页中实现双时间选择功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值