Ruby on Railsの日付操作まとめ

現在日付/時刻

```ruby
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

特定の日付

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

●日前/●日後

```ruby
[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)>
```
●ヶ月前/●ヶ月後

```ruby
[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)>
```
●年前/●年後

```ruby
[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

```ruby
[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
```

月初/月末

```ruby
[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
```


年始/年末

```ruby
[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
```

週明け/週末

```ruby
[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になるよう・・・）

```ruby
[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
```

●曜日

```ruby
[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
```

フォーマット

```ruby
[41] pry(main)> Time.current.strftime("%Y-%m-%d %T")
=> "2021-03-25 12:09:26"
```
https://docs.ruby-lang.org/ja/latest/method/Time/i/strftime.html

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

```ruby
[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
```