Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 540
morrime Avatar asked Dec 15 '22 03:12

morrime


1 Answers

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
like image 162
steenslag Avatar answered Jan 06 '23 05:01

steenslag