LoginSignup
1

More than 5 years have passed since last update.

日付の範囲指定を行う際ワンポイント注意

Posted at

日付の範囲指定

  • 2日以上経過した注文を取得する
order.rb
orders = Order.where(shipped_date: 2.business_day.ago.all_day).all
  • 2日前のその日だけしか取得していなかった。

営業日計算に利用する gem business_timeを使うときは以下の設定を

Gemfile
gem 'business_time'

初期状態で営業日を計算してみると1日と設定しても2日後の日付が表示される。
営業時間の設定がおかしいらしい、また今日は月曜日で金曜日まで祝日はない。

console
1.business_days.after(DateTime.now)
# => 2日後の日付が表示される。
root/config/initializers/business_time.rb
BusinessTime::Config.load("#{Rails.root}/config/business_time.yml")

以下のファイルで会社の営業日と営業時間を設定しておく必要がある。

root/config/business_time.yml
business_time:
  beginning_of_workday: "00:00:00" 
  end_of_workday: "23:59:59" 
  work_week:
    - mon
    - tue
    - wed
    - thu
    - fri
console
1.business_days.after(DateTime.now)
# => 1日後の日付が表示された。

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
1