Hi,
I''m subtracting date of birth from date of death to calculate age using
the following formula. (Actually, this formula is using Time.now,
because date of death is in another Controller and I haven''t figured
out how to access it from where date of birth lives. Ideas?) Anyway,
this formula works fine if the age is "years", but in the case of ages
less than a year, it just returns 0. I''d like for it to return
minutes, days, weeks, months, or years -- depending on the age. Ideas
and suggestions always welcome!! Thank you! ~Ali
def age
@age ||= begin
a = read_attribute(:age)
if a.nil?
m = read_attribute(:dob)
if not m.nil?
now = Time.now
now.year - dob.year - (dob.to_time.change(:year =>
now.year) > now ? 1 : 0)
end
else
a
end
end
end
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
2006/12/14, Ali <awilliams@washoecounty.us>:> I'm subtracting date of birth from date of death to calculate age using > the following formula. (Actually, this formula is using Time.now, > because date of death is in another Controller and I haven't figured > out how to access it from where date of birth lives. Ideas?) Anyway, > this formula works fine if the age is "years", but in the case of ages > less than a year, it just returns 0. I'd like for it to return > minutes, days, weeks, months, or years -- depending on the age. Ideas > and suggestions always welcome!! Thank you! ~Ali > > def age > > @age ||= begin > a = read_attribute(:age) > if a.nil? > m = read_attribute(:dob) > if not m.nil? > now = Time.now > now.year - dob.year - (dob.to_time.change(:year => > now.year) > now ? 1 : 0) > end > else > a > end > end > endTime.now - dob.to_time == age in seconds http://www.ruby-doc.org/core/classes/Time.html#M000281 You can get anything else from there. Hope that helps ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---