我有一个活动,start_time
并且end_time
想要检查活动是否在“进行中”。那将是检查今天的日期是否在两个日期之间的范围内。
您将如何在函数中执行此操作?
Answers:
===
实际上,有一个操作员可以做到这一点。制作一个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
irb
示例也很好用,但是在某些实验中,交互示例并非总是正确地再现。这个更容易剪切和粘贴。
现在一切都理顺了。
.to_i
,然后再使用===
它们来查看它们是否在范围内。IMO,.coverage?
是一个更好的日期解决方案。
在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
cover?
方法
Date
以及(Date.yesterday..Date.tomorrow).cover?(Date.today) => true
===
在Ruby 1.9.2中可以正常工作。您只是忘记了.to_i
如果您使用的是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
如果您使用的是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_day
并end_of_day
提供一个简单的范围。重要的部分是include_with_range?
范围上的方法。
include_with_range
因为这不是将范围与范围进行比较的情况。可能是某种脑瘤。好的收获,谢谢@TylerRick。@heathd答案的确是最好的答案。
检查的是两个日期之间的当前日期。使用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
概要
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