8
6

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.

datetime_selectでdefaultの値を特定の時間に設定する

Posted at

#やりたいこと
申込みフォームのを開いたときdatetime_selectのデフォルトの値をその日の17時に設定したい。

#解決方法
普通にdatetime_selectを使った場合は

_form.html.haml
  .form-group
    = f.label :date, class: "col-sm-3 control-label"
    = f.datetime_select :date, {}, class: 'form-control form-control-inline'

下のように今の時間が表示される
スクリーンショット 2018-10-10 10.04.05.png

デフォルトでの時間を設定したい場合は、、、

_form.html.haml
  .form-group
    = f.label :date, class: "col-sm-3 control-label"
    = f.datetime_select :date, {default: Date.today + 19.hour}, class: 'form-control form-control-inline'

とすることで設定できる
スクリーンショット 2018-10-10 10.07.27.png

##より細かく指定したい場合は
年・・・" + hoge.year"
月・・・" + hoge.month"
日・・・" + hoge.day"
時間・・・" + hoge.hour"
分・・・" + hoge.minute"

のように書けば細かく指定できる

###例えば、、、
{default: Date.today + 17.hours + 30.minutes}みたいなふうに指定してあげれば
スクリーンショット 2018-10-10 10.17.31.png

というふうになる

#参考文献
http://railsdoc.com/references/Date
http://railsdoc.com/references/datetime_select

8
6
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
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?