如何检查日期和时间是否在一个范围内

时间:2020-12-02 23:55:04

I'm trying to create a method to determine whether or not a particular date and time (within a certain timezone) are within a range.

我正在尝试创建一个方法来确定某个特定的日期和时间(在某个时区内)是否在一个范围内。

range = between(start_time, end_time) 

start_time = "date time timezone"
end_time = "date time timezone"

Here's an example of what the range input will look like:

下面是输入范围的示例:

range => between("2013-12-25 04:45:00 -0800", "2015-12-25 5:00:01 -0800")

I want to return true or false if a particular date and time falls within this range.

如果某个日期和时间落在这个范围内,我想返回真或假。

Example particular date:

示例特定日期:

instance = "2014-01-01 16:35:45 -0800"

instance.between("2013-12-25 04:45:00 -0800", "2015-12-25 5:00:01 -0800")

I don't have much experience with time in Ruby. Any suggestions would be greatly appreciated.

我在Ruby没有太多的时间经验。如有任何建议,我们将不胜感激。

1 个解决方案

#1


8  

require "date" 
instance = DateTime.parse("2014-01-01 16:35:45 -0800")
d1       = DateTime.parse("2013-12-25 04:45:00 -0800")
d2       = DateTime.parse("2015-12-25 5:00:01 -0800")
p instance.between?( d1, d2 ) # => true

#1


8  

require "date" 
instance = DateTime.parse("2014-01-01 16:35:45 -0800")
d1       = DateTime.parse("2013-12-25 04:45:00 -0800")
d2       = DateTime.parse("2015-12-25 5:00:01 -0800")
p instance.between?( d1, d2 ) # => true