280
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Railsでの日付操作でよく使うものまとめ

対象バージョン

- ruby 2.1.2
- Rails 4.2.1

現在日時の取得

now = Time.current 

N日前・N日後

now = Time.current 

# 昨日
now.yesterday

# 翌日
now.tomorrow

# N日前,N日後
now.ago(3.days)
now.since(3.days)

Nヶ月前・Nヶ月後

now = Time.current 

# 前月
now.prev_month

# 翌月
now.next_month

# Nヶ月前,Nヶ月後
now.ago(3.month)
now.since(3.month)

N年前・N年後

now = Time.current 

# 前年
now.prev_year

# 翌年
now.next_year

# N年前,N年後
now.ago(3.years)
now.since(3.years)

その他

now = Time.current 

# 月初
now.beginning_of_month

# 月末
now.end_of_month

# 今週の最初
now.beginning_of_week

# 今週の末
now.end_of_week

# 先週の月曜日
now.prev_week(:monday)

# 翌週の月曜日
now.next_week(:monday)

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
280
Help us understand the problem. What are the problem?