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.

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

> 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)

---- 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

> ---- > 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).

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.

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.

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

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

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.

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?

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:

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?

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