3
7

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.

【Rails】Ruby on Railsで時間を15分単位で丸める、区切る【初心者メモ】

Last updated at Posted at 2019-11-13

rounding gemを使う

Gemfile
gem 'rounding'
bundle install
Time.current
# => Wed, 13 Nov 2019 15:31:30 JST +09:00

Time.current.floor_to(15.minutes)
# => Wed, 13 Nov 2019 15:30:00 JST +09:00

floor_to無し
Wed, 13 Nov 2019 15:31:30 JST +09:00

floor_to有り
Wed, 13 Nov 2019 15:30:00 JST +09:00

00分、15分、30分、45分で区切られて、はみ出した分は切り捨てられる。
上記は31分30秒の30分以降が切り捨てられて、30分00秒になる。

15.minutesの部分を変えれば、好きな時間単位で丸められる。
例えば、10.minutesにすれば10分区切りにできる

3
7
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
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?