ruby 如何知道今天的日期是否在日期范围内?

yks3o0rb  于 2023-04-05  发布在  Ruby
关注(0)|答案(9)|浏览(173)

我有一个start_timeend_time的事件,想检查事件是否“正在进行中”。这将是检查今天的日期是否在这两个日期之间的范围内。
在函数中如何做到这一点?

toe95027

toe950271#

在Ruby 1.9.2中,===不工作,我得到一个错误:

irb(main):019:0> (Time.now .. (Time.now+1)) === Time.now
TypeError: can't iterate from Time
    from (irb):19:in `each'
    from (irb):19:in `include?'
    from (irb):19:in `include?'
    from (irb):19:in `==='
    from (irb):19
    from /opt/ruby192/bin/irb:12:in `<main>'

使用#cover?

irb(main):002:0> (Time.now..Time.now+4).cover?(Time.now)
=> true
irb(main):003:0> (Time.now..Time.now+4).cover?(Time.now+10)
=> false
2ledvvac

2ledvvac2#

使用===

实际上,有一个操作符可以做到这一点。创建一个Range,并使用===操作符将Time对象与它进行比较。

start   = Time.now.to_i

range   = start..(start + 2)
inside  = start + 1
outside = start + 3        # ok, now...

range === inside  # true
range === outside # false
  • 更新post-comment-flood:* 这个版本在任何地方都能很好地工作。(在Rails中,在Ruby 1中,在Ruby 2中。)早期的irb示例也能很好地工作,但在一些实验中,交互式示例并不总是正确地复制。这个更容易剪切和粘贴。

现在都解决了。

tyu7yeag

tyu7yeag3#

如果你使用的是Rails,你可以使用TimeWithZone#between?。你会得到这样的东西:

> start_time = Time.zone.parse('12pm')      => Thu, 26 Jul 2012 12:00:00 EDT -04:00
> end_time = start_time + 1.hour            => Thu, 26 Jul 2012 13:00:00 EDT -04:00
> inside = Time.zone.parse('12:30pm')       => Thu, 26 Jul 2012 12:30:00 EDT -04:00
> outside = Time.zone.parse('1:30pm')       => Thu, 26 Jul 2012 13:30:00 EDT -04:00
> inside.between?(start_time, end_time)     => true
> outside.between?(start_time, end_time)    => false
jtjikinw

jtjikinw4#

因为date类包含Comparable模块,所以每个date对象都有一个between?方法。

require 'date'

today           = Date.today
tomorrow        = today + 1
one_month_later = today >> 1

tomorrow.between?(today, one_month_later) # => true
fcg9iug3

fcg9iug35#

如果你正在使用Rails,你可以试试这个:

ruby-1.8.7-p299 :015 > a = DateTime.now
 => Fri, 02 Dec 2011 11:04:24 -0800 
ruby-1.8.7-p299 :016 > (a.beginning_of_day..a.end_of_day).include_with_range? a
 => true 
ruby-1.8.7-p299 :017 > (a.beginning_of_day..a.end_of_day).include_with_range? a+10.days
 => false 
ruby-1.8.7-p299 :018 > (a.beginning_of_day..a.end_of_day).include_with_range? a+25.hours
 => false 
ruby-1.8.7-p299 :019 > (a.beginning_of_day..a.end_of_day).include_with_range? a+2.hours
 => true

注意:我只是使用beginning_of_dayend_of_day来提供一个简单的range。重要的部分是Range上的include_with_range?方法。

sshcrbum

sshcrbum6#

摘要

d1      = DateTime.parse('2018/04/01')
  d2      = DateTime.parse('2018/04/29')
  outside = DateTime.parse('2018/04/30')
  inside  = DateTime.parse('2018/04/15')

  # include?
  (d1...d2).include?(d1)      # true
  (d1...d2).include?(d2)      # false
  (d1...d2).include?(outside) # false
  (d1...d2).include?(inside)  # true

  (d1..d2).include?(d1)      # true
  (d1..d2).include?(d2)      # true
  (d1..d2).include?(outside) # false
  (d1..d2).include?(inside)  # true

  # ===
  (d1...d2) === d1      # true
  (d1...d2) === d2      # false
  (d1...d2) === outside # false
  (d1...d2) === inside  # true

  (d1..d2) === d1      # true
  (d1..d2) === d2      # true
  (d1..d2) === outside # false
  (d1..d2) === inside  # true

  # cover?
  (d1...d2).cover?(d1)      # true
  (d1...d2).cover?(d2)      # false
  (d1...d2).cover?(outside) # false
  (d1...d2).cover?(inside)  # true

  (d1..d2).cover?(d1)      # true
  (d1..d2).cover?(d2)      # true
  (d1..d2).cover?(outside) # false
  (d1..d2).cover?(inside)  # true

  # between?
  d1.between?(d1, d2)       # true
  d2.between?(d1, d2)       # true
  outside.between?(d1, d2)  # false
  inside.between?(d1, d2)   # true
r6hnlfcb

r6hnlfcb7#

如果它们是时间戳:

def in_progress?
  (start_time..end_time).include?(Time.now)
end
v64noz0r

v64noz0r8#

选中的是两个日期之间的当前日期。使用Ruby

currentDate = DateTime.now
start_date = "2017-03-31"
end_date = "2018-03-31"
currentDate.between?(start_date, end_date)

**Out Put Will be** true or false
gywdnpxw

gywdnpxw9#

这可能会帮助一些人在那里。
ruby只能理解计算之间的日期,如果日期格式是这样的:

.strftime('%Y/%m/%d')

.strftime('%d/%m/%Y')

如果你做错了,它会给予你一个假阳性。
这个现在很好用

if tmpadd182.between?(datebeginingmonth, dateendofmonth)

相关问题