Date Field convert to Seconds since Epoch

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.

On Jun 28, 10:13 pm, Tom S. [email protected] wrote:

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)

Fred

On Sat, 2008-06-28 at 14:26 -0700, Frederick C. wrote:

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)


isn’t the result also a ‘Date’ ? wouldn’t you need to convert
that .to_i to get the integer and not a date?

Craig


isn’t the result also a ‘Date’ ? wouldn’t you need to convert
that .to_i to get the integer and not a date?

Nope. it’s a number of days (it’s an instance of Rational rather than
an integer but that doesn’t matter).

Fred

On Sat, 2008-06-28 at 15:30 -0700, Frederick C. wrote:


isn’t the result also a ‘Date’ ? wouldn’t you need to convert
that .to_i to get the integer and not a date?

Nope. it’s a number of days (it’s an instance of Rational rather than
an integer but that doesn’t matter).


I suppose I could have opened a console to see…

today = Date.today
=> #<Date: 4909291/2,0,2299161>

yesterday = Date.today - 1
=> #<Date: 4909289/2,0,2299161>

yesterday.to_s
=> “06/27/2008”

today - yesterday
=> Rational(1, 1)

:wink:

Craig

On Sun, 2008-06-29 at 08:35 -0700, Tom S. wrote:

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.

Craig

In that case, does anyone know how to convert a MySQL date to a Ruby
date?

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.

On Sun, 2008-06-29 at 09:26 -0700, Tom S. wrote:

In that case, does anyone know how to convert a MySQL date to a Ruby
date?


that should be automatic just by retrieving a date field with
activerecord.

You seem to be interchanging Date and Time and they are actually
distinct Classes

I gather what you are storing is a time and you probably need to convert
it to a Date…

thistime = Time.now
=> Sun Jun 29 09:35:54 -0700 2008

thistime = Time.now.to_date
=> #<Date: 4909293/2,0,2299161>

thistime = Time.now.to_date.to_s
=> “06/29/2008”

Craig

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?

On Sun, 2008-06-29 at 10:06 -0700, Tom S. wrote:

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

end

and I can use in a view or within the controller…

@placement.discharge_date_relative_to_today

Craig

On Sun, 2008-06-29 at 11:51 -0700, Tom S. wrote:

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?


of course.

some_date = “2008-06-29”
=> “2008-06-29”

some_date.to_date
=> #<Date: 4909293/2,0,2299161>

some_date = (“2008-06-29”).to_date
=> #<Date: 4909293/2,0,2299161>

some_date.strftime("%m-%d-%Y")
=> “06-29-2008”

why don’t you show us what you’re trying to do in script/console and
what you hope to do?

Craig

No, wait. I’m a dufus. You were right, I was confusing Date and
time :smiley: 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 :smiley:

Right as CRAIG said ,use the STRFTIME (string format time
function)…check ruby API for more options.