LoginSignup
3
2

More than 5 years have passed since last update.

月の第何週の何曜日か指定して求めるdate_supportというgemを作った

Posted at

Date型のオブジェクトに対してその月の第何週か指定して取得するメソッドと、何曜日か指定して取得するメソッドを追加するdate_supportというgemを作りました。この2つのメソッドを組み合わせると、元の日付に対して第何週の何曜日という形で日付を取得できます。

日付は不定で週と曜日が決まっている場合にすぐに求められて便利です。

date = Date.new(2015, 5, 13)

# 曜日は同じまま、その月の第2週の日付を取得
date.nth_week_of_month(2)  # => <Date: 2015-05-06>

# 第3週のまま、その週の金曜日の日付を取得
date.day_to(:friday)  # => <Date: 2015-05-15>

# その月の第4週の土曜日を取得
date.nth_week_of_month(2).day_to(:saturday)  # => <Date: 2015-05-23>
3
2
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
3
2