29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コピペで動く bootstrap-datetimepicker

Posted at

rails-bootstrap-formsを使っていてbootstrap-datetimepickerを使ったメモ書きです。
cdn経由でcssとjsを指定するのでコピペで動きます。

Bootstrap datetimepicker Date/time picker widget for Twitter Bootstrap v3
http://eonasdan.github.io/bootstrap-datetimepicker/

Moment.js
http://momentjs.com/

cssとjavascriptの読み込み。

 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.0.0/css/bootstrap-datetimepicker.min.css" />
 <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.6.0/moment.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.6.0/lang/ja.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.0.0/js/bootstrap-datetimepicker.min.js"></script>

フィールドはこんな感じ。

<%= f.date_field :date %>
<%= f.time_field :start_time %>

date-pickerとtime-pickerを別々に。

$(document).ready(function() {
  $.fn.datetimepicker.dates = {
      ja: {
        days: ["日曜", "月曜", "火曜", "水曜", "木曜", "金曜", "土曜", "日曜"],
        daysShort: ["", "", "", "", "", "", "", ""],
        daysMin: ["", "", "", "", "", "", "", ""],
        months: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        monthsShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
      }
  };
  $('input[type=date]').datetimepicker({
    format: 'YYYY-MM-DD',
    language: 'ja',
    autoclose: 'true',
    pickTime: false,
  });
  $('input[type=time]').datetimepicker({
    format: "HH:mm",
    language: 'ja',
    autoclose: 'true',
    minuteStepping:10,
    pickDate: false,
  });
});

rails-bootstrap-formsはこれ。
https://github.com/bootstrap-ruby/rails-bootstrap-forms

29
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?