Hi, I was wandering if anyone could help me with this problem:
Right now I am using a field in my database which is of type
‘Date’ (in the form dd-mm-yyyy). I would like to convert this value
(for example 26-06-08) into a number of seconds since epoch or into
another form so I can compare it with todays date (from Date.now) so
that I can tell the difference between the two dates (i.e. so I have a
value which tells me how long since the date in the database or how
much longer till we reach it.)
Any help with this matter is greatly apreciated. If any clarification
is wanted, please ask.
Hi, I was wandering if anyone could help me with this problem:
Right now I am using a field in my database which is of type
‘Date’ (in the form dd-mm-yyyy). I would like to convert this value
(for example 26-06-08) into a number of seconds since epoch or into
another form so I can compare it with todays date (from Date.now) so
that I can tell the difference between the two dates (i.e. so I have a
value which tells me how long since the date in the database or how
much longer till we reach it.)
If you’ve got two instances of Date you can just subtract them to get
the number of days separating them (or am I missing something)
My issue isn’t subtracting two dates from Time.now but subtracting one
date from the database and another from Time.now. So what I would like
to do is somthing like this:
today = Time.today
dateFromDatabase - today
I’m not sure the database query returns the date in the correct
datatype.
It’s going to return the date in whatever format it’s stored…but you
can convert it as needed, probably in the model itself.
My issue isn’t subtracting two dates from Time.now but subtracting one
date from the database and another from Time.now. So what I would like
to do is somthing like this:
today = Time.today
dateFromDatabase - today
I’m not sure the database query returns the date in the correct
datatype.
What I am storing is a date (I am using a date field type in the
database) and I want to subtract Date.today from it. If I use
model.date.to_date it returns “expected numeric or date” so I assume
that I am not getting a date from the database but a string (or
somthing else) instead.
Ok. This doesn’t seem to be going anywhere. The issue isn’t the actual
method to work out the difference between two dates but the fact that
Rails seems to be getting a string from the datebase even though the
column in the database itself is a date column (not time or datetime).
Is it possible to create a new date object in Ruby using a day, month
and year that I specify?
What I am storing is a date (I am using a date field type in the
database) and I want to subtract Date.today from it. If I use
model.date.to_date it returns “expected numeric or date” so I assume
that I am not getting a date from the database but a string (or
somthing else) instead.
I would make certain that you are storing a date and not time in the
database but I do believe that you can handle this within the model
itself.
for example…(Placement model includes a column called discharge_date
and it is indeed a ‘date’ field)
class Placement < ActiveRecord::Base
def self.discharge_date_relative_to_today
Date.today - self.discharge_date
end
Ok. This doesn’t seem to be going anywhere. The issue isn’t the actual
method to work out the difference between two dates but the fact that
Rails seems to be getting a string from the datebase even though the
column in the database itself is a date column (not time or datetime).
Is it possible to create a new date object in Ruby using a day, month
and year that I specify?
No, wait. I’m a dufus. You were right, I was confusing Date and
time I was using Time not date. So now I use “model.date -
Date.today” and it works. Thanks for all your help. I learned quite a
lot here