morrime morrime - 1 year ago 170
Ruby Question

Ruby: How to check if date and time are within a range

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.

Answer Source
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 )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download