Comparison of time

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

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

On 10 March 2010 10:17, Newb N. [email protected] 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?

On 10 March 2010 10:42, Newb N. [email protected] 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

my date field in the varchar datatype

Thank you for your time…

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

Nitin.

On 10 March 2010 10:42, Newb N. [email protected] 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs