112
103

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.

[ruby] 祝日判断や休日判断をrubyでやるなら、これでいいんじゃないかな - メモ

Last updated at Posted at 2014-12-15

business_time gem

bokmann/business_time

business_time gemが便利そう。
このgemを使うと、Dateに対していろいろなmethodが使えるようになる。

[4] pry(#<Date>):1> Date.new(2015,1,2).workday?
=> true
[5] pry(#<Date>):1> Date.today.workday?
=> true
[6] pry(#<Date>):1> Date.today.weekday?
=> true
[7] pry(#<Date>):1> Date.today.monday?
=> true

# 特定の日付までの営業日を計算
[8] pry(#<Date>):1> Date.today.business_days_until(1.month.since)
=> 21
  • 創立記念日などの、会社独自の祝日もカスタマイズ可能
BusinessTime::Config.holidays << Date.parse('会社独自の休みなど')
  • config fileも作れる
rails generate business_time:config
  • configに日本の祝日リストを持っておけば、判断methodを別途作成しなくても、Dateに生えるのが便利
business_time:
  beginning_of_workday: 9:00 am
  end_of_workday: 5:00 pm
  holidays:
    - January 01, 2014
    - January 13, 2014
    - February 11, 2014
    - March 21, 2014
    - April 29, 2014
    - May 03, 2014
    - May 05, 2014
    - May 06, 2014
    - July 21, 2014
    - September 15, 2014
    - September 23, 2014
    - October 13, 2014
    - November 03, 2014
    - November 24, 2014
    - December 23, 2014
    - January 01, 2015
    - January 12, 2015
    - February 11, 2015
    - March 21, 2015
    - April 29, 2015
    - May 03, 2015
    - May 04, 2015
    - May 05, 2015
    - May 06, 2015
    - July 20, 2015
    - September 21, 2015
    - September 23, 2015
    - October 12, 2015
    - November 03, 2015
    - November 23, 2015
    - December 23, 2015
    - January 01, 2016
    - January 11, 2016
    - February 11, 2016
    - March 21, 2016
    - April 29, 2016
    - May 03, 2016
    - May 04, 2016
    - May 05, 2016
    - July 18, 2016
    - August 11, 2016
    - September 19, 2016
    - September 22, 2016
    - October 10, 2016
    - November 03, 2016
    - November 23, 2016
    - December 23, 2016
  work_week:
    - mon
    - tue
    - wed
    - thu
    - fri
[12] pry(#<Date>):1> Date.new(2015,1,1).workday?
=> false
[13] pry(#<Date>):1> Date.new(2016,12,23).workday?
=> false
112
103
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
112
103

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?