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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10 March 2010 10:17, Newb Newb <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Newb Newb 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 youthe below is my sample data of my date field 01:21:09 pm -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
my date field in the varchar datatype Thank you for your time... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10 March 2010 10:42, Newb Newb <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Wed, Mar 10, 2010 at 3:47 PM, Newb Newb <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10 March 2010 10:42, Newb Newb <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > > > my date field in the varchar datatypeI would suggest changing the fields to DateTime. Then you will be able to do direct comparisons. Colin> > Thank you for your time... > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.