LoginSignup
35
37

More than 5 years have passed since last update.

bootstrap-datepickerを使って年月のみ取得する

Posted at

jQueryのみを使っていた頃は、jQueryのDatePickerを改造したympickerを使っていたのですが、Bootstrap3を導入したらcssがうまく適用されなかったので、bootstrap-datepickerで同じようなことができないか調べてみました。

実はbootstrap-datepickerにはモードがある

ソースコードを読んでいけばわかるのですが、bootstrap-datepickerの設定時のオプションに、minValueModeというのがあります。

bootstrap-datepicker.jsの175行目あたり
switch (o.minViewMode){
    case 1:
    case 'months':
        o.minViewMode = 1;
        break;
    case 2:
    case 'years':
        o.minViewMode = 2;
        break;
    default:
        o.minViewMode = 0;
}

これを設定してみましょう。

使ってみる

idがyear_month_start, year_month_endの要素がある場合、年月のみのdatepickerを設定してみます。

coffeescript
if $('#year_month_start, #year_month_end').is '*'
  $('#year_month_start, #year_month_end').datepicker(
    format: 'yyyy/mm'
    language: 'ja'
    autoclose: true,
    minViewMode: 'months'
  )

すると、年月のみの表示がされました!

スクリーンショット 2015-01-19 17.12.36.png

bootstrap-datepickerは便利ですなぁ。

35
37
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
35
37