You can clone with
No one assigned
I am trying to store Date and Time separately. I have two fields "startdate, :type => Date" and "starttime, :type => Time". The Date is being stored correctly from the jquery selector and Time field also stores the time correctly, but also stores today's date, ideally time should store only time right ?
Time is the Ruby object Time so the date gets stored with it.
wow that was pretty fast :), is there a work around ?
I'd store the time as a String. As long as you use 24 hour format you can still do comparison queries against it:
Person.where(:time.gt => "13:00")
@andeepak Time objects store the date. There's no way around that. If you don't want to store the date, you could store the time as a string, or in separate integer fields for the hour, minute, and second.
Thanks durran and nickhoffman, so that would mean i store the timezone info separately as well
Yup. So it's a lot easier just to store a DateTime.
I've been struggling with this issue too. Looks like I'll use String work around...
But the thing I wonder is WHY Time field type exists while even we can't do this simple comparison??
@scaryguy It's simply the way core Ruby and Active Support are designed - not that I necessarily agree. So basically you have:
Time - Is microseconds pre or post epoch, so it is the full date + time to microseconds precision.DateTime - Is similar but is more of a wrapper for SQL DATETIME fields.Date - Is essentially a DateTime but with only year, month, day information.ActiveSupport::TimeWithZone - Is a DateTime with time zone information added and some additional DSL methods.
Simply, my personal preference is to always use Time to represent dates with times because it is considerably faster given it's core implementation is in C and not Ruby, but unfortunately there is no way in Ruby to purely represent only a time with no date information - hence the recommendation of using a String.
I made a gem some time ago for storing time without date in Mongoid (represented as seconds) - https://github.com/glebtv/mongoid_time_field