Has anyone written some code to calculate a persons age based on their date of birth?
Hi James I''ve been using this in my model. The field in the database is called "birthday" creative I know ;) def age the_date = Time.now if the_date.month < birthday.month or (the_date.month == birthday.month and the_date.day < birthday.day ) the_date.year - birthday.year - 1 else the_date.year - birthday.year end end If anyone can see any issues with this please let me know. thanx On 11/25/05, James Geary <james-QkpI4yF/CmL10XsdtD+oqA@public.gmane.org> wrote:> > Has anyone written some code to calculate a persons age based on their > date of birth? > _______________________________________________ > 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
((Date.today - birthday).to_i)/365 will give you the age in years. Hammed On 25/11/05, James Geary <james-QkpI4yF/CmL10XsdtD+oqA@public.gmane.org> wrote:> > Has anyone written some code to calculate a persons age based on their > date of birth? > _______________________________________________ > 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
On 11/26/05, Hammed Malik <hammed-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > ((Date.today - birthday).to_i)/365 will give you the age in years.Much Nicer. Thanx Hammed> > On 25/11/05, James Geary <james-QkpI4yF/CmL10XsdtD+oqA@public.gmane.org> wrote: > > > > Has anyone written some code to calculate a persons age based on their > > date of birth? > > _______________________________________________ > > 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 > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Nov 25, 2005, at 3:31 AM, James Geary wrote:> Has anyone written some code to calculate a persons age based on > their date of birth?Assuming #date_of_birth returns a Date object (or nil): def age dob = date_of_birth or return nil now = Time.now now.year - dob.year - (dob.to_time.change(:year => now.year) > now ? 1 : 0) end -- Rob Leslie rob-O+g/SLjAo04@public.gmane.org
Warning: You''re going to get one off errors because of leap years. Emmett On 11/25/05, Liquid <has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 11/26/05, Hammed Malik <hammed-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ((Date.today - birthday).to_i)/365 will give you the age in years. > > Much Nicer. Thanx > > > Hammed > > > > > > > > On 25/11/05, James Geary <james-QkpI4yF/CmL10XsdtD+oqA@public.gmane.org > wrote: > > > Has anyone written some code to calculate a persons age based on their > > > date of birth? > > > _______________________________________________ > > > 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 > > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >