Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
224
Help us understand the problem. What is going on with this article?
@mmmm

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

More than 3 years have 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)

224
Help us understand the problem. What is going on with this article?
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
mmmm
Ruby/Rails/AWS

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
224
Help us understand the problem. What is going on with this article?