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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 28, 10:13 pm, Tom Savage <daboomonl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> Any help with this matter is greatly apreciated. If any clarification > is wanted, please ask.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On Sat, 2008-06-28 at 14:26 -0700, Frederick Cheung wrote:> > > On Jun 28, 10:13 pm, Tom Savage <daboomonl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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)---- 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> ---- > 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Craig White
2008-Jun-28 22:36 UTC
Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
On Sat, 2008-06-28 at 15:30 -0700, Frederick Cheung 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) ;-) Craig --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Savage
2008-Jun-29 15:35 UTC
Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig White
2008-Jun-29 16:03 UTC
Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
On Sun, 2008-06-29 at 08:35 -0700, Tom Savage 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Savage
2008-Jun-29 16:26 UTC
Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
In that case, does anyone know how to convert a MySQL date to a Ruby 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig White
2008-Jun-29 16:36 UTC
Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
On Sun, 2008-06-29 at 09:26 -0700, Tom Savage 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Savage
2008-Jun-29 17:06 UTC
Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig White
2008-Jun-29 17:31 UTC
Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
On Sun, 2008-06-29 at 10:06 -0700, Tom Savage 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Savage
2008-Jun-29 18:51 UTC
Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
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? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Savage
2008-Jun-29 18:56 UTC
Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
No, wait. I''m a dufus. You were right, I was confusing Date and time :D 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 :D --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig White
2008-Jun-29 18:56 UTC
Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
On Sun, 2008-06-29 at 11:51 -0700, Tom Savage 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
JON
2008-Jun-30 05:43 UTC
Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: ****[Rails] Re: Date Field convert to Seconds since Epoch
Right as CRAIG said ,use the STRFTIME (string format time function)..check ruby API for more options. On Jun 29, 11:56 pm, Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:> On Sun, 2008-06-29 at 11:51 -0700, Tom Savage 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--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---