Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

対象バージョン

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした