# Comparison of time

**URL:** <https://www.ruby-forum.com/t/comparison-of-time/184911>\
**Category:** Rails\
**Created:** [March 10, 2010, 11:17am UTC](https://www.ruby-forum.com/t/comparison-of-time/184911 "2010-03-10T11:17:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 11:17am UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/1 "2010-03-10T11:17:07Z")

</div>

Hello all,

I want to compare the below given times.

from\_time = ‘08:00 am’

to\_time = ‘04:30 pm’

@logtime = “ci.time \>= ‘#{from\_time}’ and ci.time \<= ‘#{@to\_time}’”

but its not working fine…giving me the wrong results…  
Any other methods or how can i try with…  
pls help me out

Thank you

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 11:33am UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/2 "2010-03-10T11:33:50Z")

</div>

Newb N. wrote:

> Hello all,
> 
> I want to compare the below given times.
> 
> from\_time = ‘08:00 am’
> 
> to\_time = ‘04:30 pm’
> 
> @logtime = “ci.time \>= ‘#{from\_time}’ and ci.time \<= ‘#{@to\_time}’”
> 
> but its not working fine…giving me the wrong results…  
> Any other methods or how can i try with…  
> pls help me out
> 
> Thank you

the below is my sample data of my date field

01:21:09 pm

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 11:35am UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/3 "2010-03-10T11:35:50Z")

</div>

On 10 March 2010 10:17, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all,  
> I want to compare the below given times.
> 
> from\_time = ‘08:00 am’  
> to\_time = ‘04:30 pm’
> 
> @logtime = “ci.time \>= ‘#{from\_time}’ and ci.time \<= ‘#{@to\_time}’”

As it stands, those variables are Strings, not Times (or DateTimes),  
and will be evaluated as strings in the SQL.

What values are you storing in the DB? Is it at Datetime format column?

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 11:54am UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/4 "2010-03-10T11:54:34Z")

</div>

On 10 March 2010 10:42, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> my date field in the varchar datatype

You’re a little bit stuffed then - as comparing greater/less than is  
comparing strings not times…

You can fudge it in your queries (assuming MySQL) using the  
“str\_to\_date” function - but roll your sleeves up, as it’s gonna be  
messy!

[http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function\_str-to-date](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date)

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 11:42am UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/5 "2010-03-10T11:42:38Z")

</div>

my date field in the varchar datatype

Thank you for your time…

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 12:09pm UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/6 "2010-03-10T12:09:47Z")

</div>

you can try distance\_of\_time\_in\_words\_to\_now…  
Read this  
[http://apidock.com/rails/ActionView/Helpers/DateHelper/distance\_of\_time\_in\_words\_to\_now](http://apidock.com/rails/ActionView/Helpers/DateHelper/distance_of_time_in_words_to_now)

Nitin.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/7 "2010-03-10T12:54:16Z")

</div>

On 10 March 2010 10:42, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> my date field in the varchar datatype

I would suggest changing the fields to DateTime. Then you will be  
able to do direct comparisons.

Colin

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 10, 2010, 9:16pm UTC](https://www.ruby-forum.com/t/comparison-of-time/184911/8 "2010-03-10T21:16:07Z")

</div>

Colin L. wrote:

> I would suggest changing the fields to DateTime. Then you will be  
> able to do direct comparisons.

From someone who has been burned by this in the past, I have a slightly  
different suggestion.

If you actually do need to store a time that is independent of a date, I  
would recommend storing time as an offset. Something like seconds from  
midnight. Store this value as an integer in the database.

Using such a technique eliminates dependency on the geo-political rules  
governing the representation of dates and times.

For convenience you could add an instance method to give you the time of  
day as a string.

# Assume the “time\_offset” column contains 25000 (seconds from midnight)

def presentable\_time  
t = Date.today.to\_time + self.time\_offset.seconds  
t.strftime("%I:%M %p")  
end

puts my\_object.presentable\_time  
=\> “06:56 AM”

With this technique your “difference” or “duration” calculations become  
trivial:

time\_diff\_seconds = obj\_1.time\_offset - obj\_2.time\_offset

It’s not that common that one needs to store time independently of date.  
However, one case that comes to mind would be if you were calculating  
statistics based on time of day. Maybe you would want a graph showing  
activity based on the time of day. If you’re doing something like that  
then having the time of day stored separately from the date would  
simplify the queries.

select \* from orders where time\_offset between 46800 and 54000;

would select all orders placed between 1:00 PM and 3:00 PM
