I am trying to calculate the number of days between 2 dates using Time.now - object.date where object.date is a date pulled from a database. I want to display the age of the entry in the database in days. I keep getting an error about not being able to convert a date into a float. What am I doing wrong. If I use object.date - object.date it seems to work but the answer is useless (0). I am doing all of this in the view. Justin -- Posted via http://www.ruby-forum.com/.
Justin Kay wrote:> I am trying to calculate the number of days between 2 dates using > Time.now - object.date where object.date is a date pulled from a > database. I want to display the age of the entry in the database in > days. I keep getting an error about not being able to convert a date > into a float. What am I doing wrong. If I use object.date - > object.date it seems to work but the answer is useless (0). I am doing > all of this in the view. > > JustinA noobs guess: Do you mean object.to_time instead of object.date? -- Posted via http://www.ruby-forum.com/.
Jim wrote:> Justin Kay wrote: >> I am trying to calculate the number of days between 2 dates using >> Time.now - object.date where object.date is a date pulled from a >> database. I want to display the age of the entry in the database in >> days. I keep getting an error about not being able to convert a date >> into a float. What am I doing wrong. If I use object.date - >> object.date it seems to work but the answer is useless (0). I am doing >> all of this in the view. >> >> Justin > > A noobs guess: > > Do you mean object.to_time instead of object.date?no, in object.date, date is the name of the date field of the class "object". -- Posted via http://www.ruby-forum.com/.
Justin Kay wrote:> Jim wrote: >> Justin Kay wrote: >>> I am trying to calculate the number of days between 2 dates using >>> Time.now - object.date where object.date is a date pulled from a >>> database. I want to display the age of the entry in the database in >>> days. I keep getting an error about not being able to convert a date >>> into a float. What am I doing wrong. If I use object.date - >>> object.date it seems to work but the answer is useless (0). I am doing >>> all of this in the view. >>> >>> Justin >> >> A noobs guess: >> >> Do you mean object.to_time instead of object.date? > > no, in object.date, date is the name of the date field of the class > "object".What do you get if you put this in your view?? <%= debug(object) %> <%= object.date.class %> -- Posted via http://www.ruby-forum.com/.
Time is time. Date is date. You can try (Date.today - object.date).to_i On 1/22/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:> Justin Kay wrote: > > Jim wrote: > >> Justin Kay wrote: > >>> I am trying to calculate the number of days between 2 dates using > >>> Time.now - object.date where object.date is a date pulled from a > >>> database. I want to display the age of the entry in the database in > >>> days. I keep getting an error about not being able to convert a date > >>> into a float. What am I doing wrong. If I use object.date - > >>> object.date it seems to work but the answer is useless (0). I am doing > >>> all of this in the view. > >>> > >>> Justin > >> > >> A noobs guess: > >> > >> Do you mean object.to_time instead of object.date? > > > > no, in object.date, date is the name of the date field of the class > > "object". > > What do you get if you put this in your view?? > > <%= debug(object) %> > <%= object.date.class %> > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Blog >>> http://spaces.msn.com/members/skyincookoo
Sky Yin wrote:> Time is time. Date is date. You can try (Date.today - object.date).to_i > > On 1/22/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote: >> >>> >> >> <%= debug(object) %> >> <%= object.date.class %> >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Blog >>> http://spaces.msn.com/members/skyincookoo$ ./script/console>> "2006-1-12".to_time=> Thu Jan 12 00:00:00 UTC 2006 So in this case, date is Time. -- Posted via http://www.ruby-forum.com/.
No, cause you cast it to Time. If you want a Date:>> "2006-1-12".to_date=> #<Date: 4907495/2,0,2299161> On 1/22/06, Jim <narf968@gmail.com> wrote:> Sky Yin wrote: > > Time is time. Date is date. You can try (Date.today - object.date).to_i > > > > On 1/22/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote: > >> >>> > >> > >> <%= debug(object) %> > >> <%= object.date.class %> > >> > >> -- > >> Posted via http://www.ruby-forum.com/. > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > > > > > -- > > Blog >>> http://spaces.msn.com/members/skyincookoo > > $ ./script/console > >> "2006-1-12".to_time > => Thu Jan 12 00:00:00 UTC 2006 > > So in this case, date is Time. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Sky Yin wrote:> No, cause you cast it to Time. If you want a Date: > >>> "2006-1-12".to_date > => #<Date: 4907495/2,0,2299161>Thanks for the hint. I needed to cast Time.now to date and it worked fine. I was trying to use a time - date and that didn''t work very well. So: Time.now.to_date - object.created_on = success! -- Posted via http://www.ruby-forum.com/.
Am Sonntag, den 22.01.2006, 15:41 +0100 schrieb Justin Kay:> Time.now.to_date - object.created_on = success!If object.created_on is a Date object why not use: Date.today - object.created_on = classiness! But if it is a Time object as provided by the Activerecord::Timestamp Module you should use: ((Time.now - object.created_on) / (60*60*24)).floor -- Norman Timmler http://blog.inlet-media.de