0
0

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 3 years have passed since last update.

Ruby on Railsの日付操作まとめ

Posted at

現在日付/時刻

pry(main)> Time.current
=> Thu, 25 Mar 2021 11:36:35 JST +09:00
pry(main)> 0.days.ago
=> Thu, 25 Mar 2021 12:16:53 JST +09:00

nowよりcurrentを使うほうが良いらしい。
https://qiita.com/kodai_0122/items/111457104f83f1fb2259

特定の日付

pry(main)> Time.new(2021, 3, 25, 11, 22, 33, 00)
=> 2021-03-25 11:22:33 +0000

●日前/●日後

[9] pry(main)> Time.current.yesterday
=> Wed, 24 Mar 2021 11:47:08 JST +09:00
[10] pry(main)> Time.current.tomorrow
=> Fri, 26 Mar 2021 11:47:16 JST +09:00
[11] pry(main)> Time.current.ago(3.days)
=> Mon, 22 Mar 2021 11:47:28 JST +09:00
[12] pry(main)> Time.current.since(3.days)
=> Sun, 28 Mar 2021 11:47:43 JST +09:00
[13] pry(main)>

●ヶ月前/●ヶ月後

[13] pry(main)> Time.current.last_month
=> Thu, 25 Feb 2021 11:50:33 JST +09:00
[14] pry(main)> Time.current.next_month
=> Sun, 25 Apr 2021 11:50:41 JST +09:00
[15] pry(main)> Time.current.ago(3.month)
=> Fri, 25 Dec 2020 11:50:52 JST +09:00
[16] pry(main)> Time.current.since(3.month)
=> Fri, 25 Jun 2021 11:50:59 JST +09:00
[17] pry(main)>

●年前/●年後

[17] pry(main)> Time.current.last_year
=> Wed, 25 Mar 2020 11:52:07 JST +09:00
[18] pry(main)> Time.current.next_year
=> Fri, 25 Mar 2022 11:52:12 JST +09:00
[19] pry(main)> Time.current.ago(3.year)
=> Sun, 25 Mar 2018 11:52:26 JST +09:00
[20] pry(main)> Time.current.since(3.year)
=> Mon, 25 Mar 2024 11:52:34 JST +09:00

0:00/23:59

[23] pry(main)> Time.current.beginning_of_day
=> Thu, 25 Mar 2021 00:00:00 JST +09:00
[24] pry(main)> Time.current.end_of_day
=> Thu, 25 Mar 2021 23:59:59 JST +09:00

月初/月末

[25] pry(main)> Time.current.beginning_of_month
=> Mon, 01 Mar 2021 00:00:00 JST +09:00
[26] pry(main)> Time.current.end_of_month
=> Wed, 31 Mar 2021 23:59:59 JST +09:00

年始/年末

[27] pry(main)> Time.current.beginning_of_year
=> Fri, 01 Jan 2021 00:00:00 JST +09:00
[28] pry(main)> Time.current.end_of_year
=> Fri, 31 Dec 2021 23:59:59 JST +09:00

週明け/週末

[29] pry(main)> Time.current.beginning_of_week
=> Mon, 22 Mar 2021 00:00:00 JST +09:00
[30] pry(main)> Time.current.end_of_week
=> Sun, 28 Mar 2021 23:59:59 JST +09:00

先週/来週(何故か時間は月曜0:00になるよう・・・)

[31] pry(main)> Time.current.last_week
=> Mon, 15 Mar 2021 00:00:00 JST +09:00
[32] pry(main)> Time.current.next_week
=> Mon, 29 Mar 2021 00:00:00 JST +09:00

●曜日

[38] pry(main)> Time.current.beginning_of_week(:wednesday)
=> Wed, 24 Mar 2021 00:00:00 JST +09:00
[39] pry(main)> Time.current.last_week(:wednesday)
=> Wed, 17 Mar 2021 00:00:00 JST +09:00
[40] pry(main)> Time.current.next_week(:wednesday)
=> Wed, 31 Mar 2021 00:00:00 JST +09:00

フォーマット

[41] pry(main)> Time.current.strftime("%Y-%m-%d %T")
=> "2021-03-25 12:09:26"

範囲
範囲をwhere句に渡すとbetweenで検索してくれるらしい

[50] pry(main)> Time.current.all_day
=> Thu, 25 Mar 2021 00:00:00 JST +09:00..Thu, 25 Mar 2021 23:59:59 JST +09:00
[51] pry(main)> Time.current.all_month
=> Mon, 01 Mar 2021 00:00:00 JST +09:00..Wed, 31 Mar 2021 23:59:59 JST +09:00
[52] pry(main)> Time.current.all_year
=> Fri, 01 Jan 2021 00:00:00 JST +09:00..Fri, 31 Dec 2021 23:59:59 JST +09:00
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?