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

日付比較の覚え方とDate.todayについて

Last updated at Posted at 2018-08-11

#日付の比較について

日付を比較する時大なり小なりがどっちかなかなか覚えられないので覚え方を自分なりに考えて見たのでメモ。

比較の覚え方
未来の方が比重が大きい(未来の方が大切)ので未来が大きければ真

未来 >  過去   true

未来の方が小さければ 偽

未来  <  過去           false

これで僕は覚えやすくなりましたwww

#Date.todayについて

何気なくDate.todayを使用していたがこれはUTCで出力されるので注意が必要
日本時間にするためにはDate.currentを使用する必要がある。

-追記-
znzさんにご指摘頂き修正しました(ありがとうございます!)

railsで使用する際、Date.tomorrowやDate.yesterdayはapplication.rbのタイムゾーンを反映するのにDate.todayは環境変数で指定したタイムゾーンを反映しており、指定がない場合はUTCになります。

application.rbで指定したタイムゾーンを使用する場合はDate.currentとする。

application.rbでのタイムゾーンの指定

config.time_zone = 'Asia/Tokyo'
config.active_record.default_timezone = :local
2
0
3

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
2
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?