【问题标题】:Given a Ruby DateTime how can I determine if it represents a day in the current work week?给定一个 Ruby DateTime 我如何确定它是否代表当前工作周中的一天?
【发布时间】:2015-10-27 14:23:16
【问题描述】:

我有一个 Ruby DateTime 实例,我想知道它在本周(周一至周日)内。我没有使用 Rails。

我的猜测是,我需要获取本周的星期一和星期日的日期,并测试我的日期是否介于两者之间。

【问题讨论】:

  • datetime.strptime - 使用它来获取一年中的星期几和星期几。

标签: ruby date


【解决方案1】:
Date.parse("Monday") # get the Monday date of current week
=> #<Date: 2015-10-26 ((2457322j,0s,0n),+0s,2299161j)>

Date.today.between?(Date.parse("Monday"), Date.parse("Monday") + 7)
=> true

DateTime.now.between?(Date.parse("Monday"),Date.parse("Monday") + 7)
=> true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多