11
7

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.

【Rails】Dateクラスチートシート : 時系列順

Posted at

Dateオブジェクトの取り方(時系列)

去年

Date.today.prev_year
Date.today - 1.year

#=> Tue, 18 Nov 2014

昨月

Date.today.prev_month
Date.today - 1.month

#=> Sun, 18 Oct 2015

昨日

Date.yesterday
Date.today - 1

#=> Tue, 17 Nov 2015

今日

Date.today
Date.current

#=> Wed, 18 Nov 2015

明日

Date.tomorrow
Date.today + 1 

#=> Thu, 19 Nov 2015

来月

Date.today.next_month
Date.today + 1.month

#=> Fri, 18 Dec 2015

来年

Date.today.next_year
Date.today + 1.year

#=> Fri, 18 Nov 2016

特定の日付

Date.new(1987, 8, 20)
Date.parse('1987-8-20')

#=> Thu, 20 Aug 1987

Dateオブジェクトのメンバーを取得する。

↓オブジェクト

d = Date.parse('2014-01-01')

#=> Mon, 21 Apr 2014

何年?


d.year 

#=>2014

何月?

d.month

#=>4

何日?

d.day

#=>21

何曜日?

d.wday

#=>1

数字と曜日の対応表

{ 
  sun: 0,
  mon: 1,
  tue: 2,
  wed: 3,
  thu: 4,
  fri: 5,
  sat: 6
}

strftime(Dateオブジェクトの整形) 

引用:strftime を憶えられない

Kobito.zGi1Yu.png

eg) strftime

d.new(2016,12,24)

d.strftime('%Y/%m/%d')
#=> "2014/04/21"

d.strftime('%Y年%m月%d日(%a)')
#=> "2014年04月21日(Mon)"
11
7
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?