LoginSignup
120
114

More than 5 years have passed since last update.

ActiveSupport Time拡張

Last updated at Posted at 2013-01-23

all_〜

  • Rangeオブジェクトを返す
Time.now
 # => 2013-01-23 19:41:24 +0900
Time.now.all_day
 # => 2013-01-23 00:00:00 +0900..2013-01-23 23:59:59 +0900
Time.now.all_week
 # => 2013-01-21 00:00:00 +0900..2013-01-27 23:59:59 +0900
Time.now.all_month
 # => 2013-01-01 00:00:00 +0900..2013-01-31 23:59:59 +0900
Time.now.all_quarter
 # => 2013-01-01 00:00:00 +0900..2013-03-31 23:59:59 +0900
Time.now.all_year
 # => 2013-01-01 00:00:00 +0900..2013-12-31 23:59:59 +0900

〜_ago

  • 指定された過去の日時を返す
Time.now
 # => 2013-01-23 19:45:18 +0900
Time.now.weeks_ago(1)
 # => 2013-01-16 19:44:42 +0900
Time.now.months_ago(1)
 # => 2012-12-23 19:46:08 +0900
Time.now.years_ago(1)
 # => 2012-01-23 19:46:16 +0900
Time.now.ago(1.week)
 # => 2013-01-16 19:46:57 +0900

〜_since

  • 指定された未来の日時を返す
Time.now
 # => 2013-01-23 19:47:54 +0900
Time.now.months_since(1)
 # => 2013-02-23 19:48:19 +0900
Time.now.years_since(1)
 # => 2014-01-23 19:48:42 +0900
Time.now.since(1.month)
 # => 2013-02-23 19:49:08 +0900

beginning_of_〜

  • 指定された日時の始めの日時を返す
Time.now
 # => 2013-01-23 19:53:31 +0900
Time.now.beginning_of_hour
 # => 2013-01-23 19:00:00 +0900
Time.now.beginning_of_day
 # => 2013-01-23 00:00:00 +0900
Time.now.beginning_of_week
 # => 2013-01-21 00:00:00 +0900
Time.now.beginning_of_month
 # => 2013-01-01 00:00:00 +0900
Time.now.beginning_of_quarter
 # => 2013-01-01 00:00:00 +0900
Time.now.beginning_of_year
 # => 2013-01-01 00:00:00 +0900

end_of_〜

  • 指定された日時の終わりの日時を返す
Time.now
 # => 2013-01-01 00:00:00 +0900
Time.now.end_of_hour
 # => 2013-01-23 19:59:59 +0900
Time.now.end_of_day
 # => 2013-01-23 19:56:25 +0900
Time.now.end_of_week
 # => 2013-01-27 23:59:59 +0900
Time.now.end_of_month
 # => 2013-01-31 23:59:59 +0900
Time.now.end_of_quarter
 # => 2013-03-31 23:59:59 +0900
Time.now.end_of_year
 # => 2013-12-31 23:59:59 +0900

next_〜

  • 指定された日時の次の日時を返す
Time.now
 # => 2013-01-23 20:00:30 +0900
Time.now.next_week
 # => 2013-01-28 00:00:00 +0900
Time.now.next_month
 # => 2013-02-23 20:00:45 +0900
Time.now.next_year
 # => 2014-01-23 20:00:53 +0900

prev_〜

  • 指定された日時の前の日時を返す
Time.now
 # => 2013-01-23 20:02:05 +0900
Time.now.prev_week
 # => 2013-01-14 00:00:00 +0900
Time.now.prev_month
 # => 2012-12-23 20:02:59 +0900
Time.now.prev_year
 # => 2012-01-23 20:03:05 +0900

その他

  • 過去判定
Time.now.past?
 # => true
Time.now.since(1.day).past?
 # => false
Time.now.ago(1.day).past?
 # => true
  • 今日判定
Time.now.today?
 # => true
Time.now.ago(1.day).today?
 # => false
Time.now.since(1.day).today?
 # => false
  • 月の日数を返す

days_in_month

Time.days_in_month( 2, 2012 )
 # => 29
Time.days_in_month( 2, 2013 )
 # => 28

※yearは省略可能

Time.days_in_month( 2 )
 # => 28
120
114
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
120
114