如何在Rails中计算下一个,上一个工作日? [英] How to calculate next, previous business day in Rails?
本文介绍了如何在Rails中计算下一个,上一个工作日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Rails 中计算下一个和上一个工作日?
How to calculate next and previous business days in Rails?
推荐答案
据我所知,这就是你要找的?(测试过了)
As far as I understand, this is what you are looking for? (tested it)
require 'date'
def next_business_day(date)
skip_weekends(date, 1)
end
def previous_business_day(date)
skip_weekends(date, -1)
end
def skip_weekends(date, inc = 1)
date += inc
while date.wday == 0 || date.wday == 6
date += inc
end
date
end
您可以按如下方式进行测试:
You can test it as follows:
begin
t = Date.new(2009,9,11) #Friday, today
puts "Today: #{Date::DAYNAMES[t.wday]} #{Date::MONTHNAMES[t.mon]} #{t.day}"
nextday = next_business_day(t)
puts "Next B-day: #{Date::MONTHNAMES[nextday.mon]} #{nextday.day}"
previousday = previous_business_day(nextday)
puts "back to previous: #{Date::MONTHNAMES[previousday.mon]} #{previousday.day}"
yesterday = previous_business_day(previousday)
puts "yesterday: #{Date::MONTHNAMES[yesterday.mon]} #{yesterday.day}"
end
这篇关于如何在Rails中计算下一个,上一个工作日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文