Hi, I wrote a little helper that calulates someone''s age based on his/her date of birth. def age(dob) diff = Date.today - dob age = (diff / 365.25).floor age.to_s end It works fine, but it''s not completely accurate as it just takes the average days in a year. It should be able to calculate this more accurately, right? I can''t work it out though.. Jeroen
Jeroen Houben wrote:> Hi, > > I wrote a little helper that calulates someone''s age based on his/her > date of birth. > > def age(dob) > diff = Date.today - dob > age = (diff / 365.25).floor > age.to_s > end > > It works fine, but it''s not completely accurate as it just takes the > average days in a year. It should be able to calculate this more > accurately, right? I can''t work it out though.. > > JeroenGive this a try Jeroen: def get_age(dob) return nil unless dob now = Time.now now.year - dob.year - (dob.to_time.change(:year => now.year) > now ? 1 : 0) end -- Posted via http://www.ruby-forum.com/.
> > >> now.year - dob.year - (dob.to_time.change(:year => now.year) > now ? > 1Nifty. It can''t handle dobs before 1970 though. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Here''s one that''s not optimized in any way, but it is accurate: def age(dob) unless dob.nil? a = Date.today.year - dob.year b = Date.new(Date.today.year, dob.month, dob.day) a = a-1 if b > Date.today return a end nil end On Sun, 18 Dec 2005, Jeroen Houben wrote:> Hi, > > I wrote a little helper that calulates someone''s age based on his/her date of > birth. > > def age(dob) > diff = Date.today - dob > age = (diff / 365.25).floor > age.to_s > end > > It works fine, but it''s not completely accurate as it just takes the average > days in a year. It should be able to calculate this more accurately, right? I > can''t work it out though.. > > Jeroen
> > >Here''s one that''s not optimized in any way, but it is accurate:Good but fails on leap days. d = Date.new(2000,2,29) age(d) #throws exception _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
What about this: def age Time.now.year - date_of_birth.year - ( ((date_of_birth.month * 31 + date_of_birth.day > Time.now.month * 31 + Time.now.day) and Time.now.year != date_of_birth.year) ? 1 : 0 ) unless date_of_birth.blank? end That should work I think. -Jonathan. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jeroen Houben Sent: Monday, 19 December 2005 12:23 a.m. To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] calculate age based on DoB Hi, I wrote a little helper that calulates someone''s age based on his/her date of birth. def age(dob) diff = Date.today - dob age = (diff / 365.25).floor age.to_s end It works fine, but it''s not completely accurate as it just takes the average days in a year. It should be able to calculate this more accurately, right? I can''t work it out though.. Jeroen _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Brian Buckley wrote:>> >> >Here''s one that''s not optimized in any way, but it is accurate: > > > Good but fails on leap days. > > d = Date.new(2000,2,29) > age(d) #throws exceptionOuch. Okay so does anyone have a method for turning a date object into an age which works for both dates before 1970 and leap days? -- Posted via http://www.ruby-forum.com/.
Sean Schertell wrote:> Brian Buckley wrote: > >>>>Here''s one that''s not optimized in any way, but it is accurate: >> >> >>Good but fails on leap days. >> >>d = Date.new(2000,2,29) >>age(d) #throws exception > > > Ouch. Okay so does anyone have a method for turning a date object into > an age which works for both dates before 1970 and leap days? >Here''s one for Brian to test: def age_at(date, dob) day_diff = date.day - dob.day month_diff = date.month - dob.month - (day_diff < 0 ? 1 : 0) date.year - dob.year - (month_diff < 0 ? 1 : 0) end Both arguments must be Dates. regards Justin
Jonathan Viney wrote:> What about this: > > def age > Time.now.year - date_of_birth.year - ( > ((date_of_birth.month * 31 + date_of_birth.day > Time.now.month * 31 > + Time.now.day) and Time.now.year != date_of_birth.year) ? 1 : 0 > ) unless date_of_birth.blank? > end > > That should work I think.It seems people are now 1 year younger than they actually are. So one could claim this is the best implementation yet. J
Justin Forder wrote:> Sean Schertell wrote: > >> Brian Buckley wrote: >> >>>>> Here''s one that''s not optimized in any way, but it is accurate: >>> >>> >>> >>> Good but fails on leap days. >>> >>> d = Date.new(2000,2,29) >>> age(d) #throws exception >> >> >> >> Ouch. Okay so does anyone have a method for turning a date object >> into an age which works for both dates before 1970 and leap days? >> > > Here''s one for Brian to test: > > def age_at(date, dob) > day_diff = date.day - dob.day > month_diff = date.month - dob.month - (day_diff < 0 ? 1 : 0) > date.year - dob.year - (month_diff < 0 ? 1 : 0) > end > > Both arguments must be Dates.Works fine for me. Also handles pre 1970 dates just fine. Thanks! Jeroen
What about this? def age(dob) unless dob.nil? years = Date.today.year - dob.year if Date.today.month < dob.month years = years + 1 end if (Date.today.month == dob.month and Date.today.day < dob.day) years = years - 1 end return years end nil end Frank
Nice ;) But are you sure it gives ages 1 year below the real age? Seems to work fine for me. -Jonny. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jeroen Houben Sent: Monday, 19 December 2005 10:08 p.m. To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] calculate age based on DoB Jonathan Viney wrote:> What about this: > > def age > Time.now.year - date_of_birth.year - ( > ((date_of_birth.month * 31 + date_of_birth.day > Time.now.month *31> + Time.now.day) and Time.now.year != date_of_birth.year) ? 1 : 0 > ) unless date_of_birth.blank? > end > > That should work I think.It seems people are now 1 year younger than they actually are. So one could claim this is the best implementation yet. J _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jonathan Viney wrote:> Nice ;) > > But are you sure it gives ages 1 year below the real age? Seems to work > fine for me.I apologize! I must have mistyped something when I put it inside my helper class. Below are two methods, including yours, both work fine. I find the first method easier to understand. It also make a little more sense to me to use just the Date class, as we won''t be dealing with time when it comes to birthdays and age. I don''t know even know what time I was born. def age2(dob) date = Date.today day_diff = date.day - dob.day month_diff = date.month - dob.month - (day_diff < 0 ? 1 : 0) date.year - dob.year - (month_diff < 0 ? 1 : 0) end def age(dob) Time.now.year - dob.year - (((dob.month * 31 + dob.day > Time.now.month * 31 + Time.now.day) and Time.now.year != dob.year) ? 1 : 0) end Jeroen> > -Jonny. > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jeroen Houben > Sent: Monday, 19 December 2005 10:08 p.m. > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] calculate age based on DoB > > Jonathan Viney wrote: > >>What about this: >> >>def age >> Time.now.year - date_of_birth.year - ( >> ((date_of_birth.month * 31 + date_of_birth.day > Time.now.month * > > 31 > >>+ Time.now.day) and Time.now.year != date_of_birth.year) ? 1 : 0 >> ) unless date_of_birth.blank? >>end >> >>That should work I think. > > > It seems people are now 1 year younger than they actually are. So one > could claim this is the best implementation yet. > > J > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jeroen Houben wrote:> > def age2(dob) > date = Date.today > day_diff = date.day - dob.day > month_diff = date.month - dob.month - (day_diff < 0 ? 1 : 0) > date.year - dob.year - (month_diff < 0 ? 1 : 0) > end >def age(dob, date=Date.today) day_diff = date.day - dob.day month_diff = date.month - dob.month - (day_diff < 0 ? 1 : 0) date.year - dob.year - (month_diff < 0 ? 1 : 0) end This is a little nicer, gives you the option to return somebody''s age at some given date. Thanks to Justin Forder for this one. Jeroen