【问题标题】:Ruby First Business Day of next monthRuby 下个月的第一个工作日
【发布时间】:2015-10-09 21:33:19
【问题描述】:

我正在尝试访问下个月的第一个工作日。例如,如果月底是周六或周日,我想吐出下周一的日期,否则,我希望它只返回该月的第一天。到目前为止,我使用

访问下个月的第一天没有问题

Time.now.end_of_month + 6.hours.

提前致谢!!

【问题讨论】:

  • 如果 1 月 1 日是星期一,那么它是工作日吗?
  • 您的意思是“工作日”,在这种情况下您必须考虑假期(例如包括these),还是“工作日”?

标签: ruby date time


【解决方案1】:
require 'date'

def next_month_1st_bday
  t = Date.today
  d = Date.new(t.year, t.month, -1)  # last day of current month
  d += 1 until d.wday.between?(1, 5) # no 0 (sunday) or 6 (saturday)
  d
end

p next_month_1st_bday # => #<Date: 2015-11-02 ((2457329j,0s,0n),+0s,2299161j)>

【讨论】:

  • 非常感谢!奇迹般有效! 拳头撞击
【解决方案2】:

business_time gem 非常适合这个。

https://github.com/bokmann/business_time

首先安装所需的gem

require 'active_support/core_ext'
require 'business_time'

现在我们可以得到当月的月底

month_end = Date.today.end_of_month

现在,要获得下个月的第一个工作日,只需执行此操作

1.business_days.after(month_end)

【讨论】:

    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多