<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Calculate age in Rails with a birthday</title>
	<atom:link href="http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/</link>
	<description>class AllYourCode &#60; Us</description>
	<lastBuildDate>Mon, 28 Jun 2010 23:07:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: CaDs</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-738</link>
		<dc:creator>CaDs</dc:creator>
		<pubDate>Fri, 12 Jun 2009 06:47:07 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-738</guid>
		<description>This seems to do the job:&lt;br&gt;&lt;br&gt;((Time.now.to_time - birthday.to_time) / 1.years).to_i</description>
		<content:encoded><![CDATA[<p>This seems to do the job:</p>
<p>((Time.now.to_time &#8211; birthday.to_time) / 1.years).to_i</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NOV</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-269</link>
		<dc:creator>NOV</dc:creator>
		<pubDate>Sat, 10 May 2008 22:20:57 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-269</guid>
		<description>today, birthday = [Date.today, self].map do &#124;d&#124; 
  d.strftime(&quot;%Y%m%d&quot;).to_i
end
(today - birthday) / 10000</description>
		<content:encoded><![CDATA[<p>today, birthday = [Date.today, self].map do |d|<br />
  d.strftime(&#8220;%Y%m%d&#8221;).to_i<br />
end<br />
(today &#8211; birthday) / 10000</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NOV</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-267</link>
		<dc:creator>NOV</dc:creator>
		<pubDate>Sat, 10 May 2008 21:56:58 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-267</guid>
		<description>&lt;pre&gt;
class Date
  def to_age
    integer_style = &quot;%Y%m%d&quot;
    today = Date.today.strftime(integer_style).to_i
    birthday = Date.today.strftime(integer_style).to_i
    (today - birthday) / 10000
  end
end
&lt;/pre&gt;
=&gt; birthday.to_age

If the birthday.class.class_name == Time, you can use below.

http://d.hatena.ne.jp/secondlife/20050922/1127376501</description>
		<content:encoded><![CDATA[<pre>
class Date
  def to_age
    integer_style = "%Y%m%d"
    today = Date.today.strftime(integer_style).to_i
    birthday = Date.today.strftime(integer_style).to_i
    (today - birthday) / 10000
  end
end
</pre>
<p>=&gt; birthday.to_age</p>
<p>If the birthday.class.class_name == Time, you can use below.</p>
<p><a href="http://d.hatena.ne.jp/secondlife/20050922/1127376501" rel="nofollow">http://d.hatena.ne.jp/secondlife/20050922/1127376501</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rolf</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-177</link>
		<dc:creator>rolf</dc:creator>
		<pubDate>Tue, 15 Apr 2008 14:03:39 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-177</guid>
		<description>&lt;pre&gt;
def age(birthday)
    y=(Date.today.year) - (birthday.year)
    m=(Date.today.month)-(birthday.month)
    d=(Date.today.day)-(birthday.day)
        case 
           when (m&lt;0) 
                      age=y-1 
           when (m=0)
                        case
                            when(d0)
                              age=y
                        end
            when (m&gt;0)
              age=y
         end
    age
  end
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<pre>
def age(birthday)
    y=(Date.today.year) - (birthday.year)
    m=(Date.today.month)-(birthday.month)
    d=(Date.today.day)-(birthday.day)
        case
           when (m&lt;0)
                      age=y-1
           when (m=0)
                        case
                            when(d0)
                              age=y
                        end
            when (m&gt;0)
              age=y
         end
    age
  end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: rolf</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-176</link>
		<dc:creator>rolf</dc:creator>
		<pubDate>Tue, 15 Apr 2008 13:56:49 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-176</guid>
		<description>I&#039;ve put the next few lines in my application_helper, I&#039;m using it to calculate the age of people today(but this could be any date), just passing @person.birthday to it. gr rolf

&lt;pre&gt;
  def age(birthday)
    y=(Date.today.year) - (birthday.year)
    m=(Date.today.month)-(birthday.month)
    d=(Date.today.day)-(birthday.day)
        case 
           when (m&lt;0) 
                      age=y-1     #april-mei=-1; april-april=0; april-maart=1#15-15=0; 15-16=-1; 15-13=2
           when (m=0)
                        case
                            when(d0)
                              age=y
                        end
            when (m&gt;0)
              age=y
         end
    age
  end
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>I&#8217;ve put the next few lines in my application_helper, I&#8217;m using it to calculate the age of people today(but this could be any date), just passing @person.birthday to it. gr rolf</p>
<pre>
  def age(birthday)
    y=(Date.today.year) - (birthday.year)
    m=(Date.today.month)-(birthday.month)
    d=(Date.today.day)-(birthday.day)
        case
           when (m&lt;0)
                      age=y-1     #april-mei=-1; april-april=0; april-maart=1#15-15=0; 15-16=-1; 15-13=2
           when (m=0)
                        case
                            when(d0)
                              age=y
                        end
            when (m&gt;0)
              age=y
         end
    age
  end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: api</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-171</link>
		<dc:creator>api</dc:creator>
		<pubDate>Fri, 11 Apr 2008 14:29:33 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-171</guid>
		<description>( (Date.today - birthday).to_i / 365.25).floor this will work with leap-year</description>
		<content:encoded><![CDATA[<p>( (Date.today &#8211; birthday).to_i / 365.25).floor this will work with leap-year</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naveen Dittakavi</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-100</link>
		<dc:creator>Naveen Dittakavi</dc:creator>
		<pubDate>Mon, 24 Mar 2008 18:41:55 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-100</guid>
		<description>Hi Tobi,

We couldn&#039;t get the above to work. Here&#039;s what worked for us in the controller:
&lt;pre&gt;
birthday = Time.mktime(params[:year], params[:month].to_i, params[:day].to_i)
@age = age(birthday)

protected

def age( birthday )
age = Date.today.year - birthday.year
today = Time.now
age -= 1 if birthday &gt; today.years_ago( age )
age
end
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Tobi,</p>
<p>We couldn&#8217;t get the above to work. Here&#8217;s what worked for us in the controller:</p>
<pre>
birthday = Time.mktime(params[:year], params[:month].to_i, params[:day].to_i)
@age = age(birthday)

protected

def age( birthday )
age = Date.today.year - birthday.year
today = Time.now
age -= 1 if birthday &gt; today.years_ago( age )
age
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobi</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-82</link>
		<dc:creator>Tobi</dc:creator>
		<pubDate>Thu, 13 Mar 2008 14:34:09 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-82</guid>
		<description>ups it should be:
&lt;pre&gt;
def age( birthday )
    age = Date.today.year - birthday.year
    age -= 1 if birthday &gt; Date.today.years_ago( age )
    age
end
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>ups it should be:</p>
<pre>
def age( birthday )
    age = Date.today.year - birthday.year
    age -= 1 if birthday &gt; Date.today.years_ago( age )
    age
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobi</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-81</link>
		<dc:creator>Tobi</dc:creator>
		<pubDate>Thu, 13 Mar 2008 14:32:23 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-81</guid>
		<description>Hey thanks for your solution. 

The exact age with leap years seams a bit tricky. Unfortunately the above solution has still 2 problems:
 1. it changes the actual birthday data which we may need later in progress
 2. it raises an &quot;InvalidDate Exception&quot; if now is a leapyear day, e.g. 2008/2/29 

I solved this with:

&lt;pre&gt;
 def age( birthday )
    return 0 unless info.birthday
    age = Date.today.year - birthday.year 
    age -= 1 if birthday &gt; Date.today.years_ago( age )
    age
  end
&lt;/pre&gt;
check http://pastie.caboo.se/165173 for the belonging tests</description>
		<content:encoded><![CDATA[<p>Hey thanks for your solution. </p>
<p>The exact age with leap years seams a bit tricky. Unfortunately the above solution has still 2 problems:<br />
 1. it changes the actual birthday data which we may need later in progress<br />
 2. it raises an &#8220;InvalidDate Exception&#8221; if now is a leapyear day, e.g. 2008/2/29 </p>
<p>I solved this with:</p>
<pre>
 def age( birthday )
    return 0 unless info.birthday
    age = Date.today.year - birthday.year
    age -= 1 if birthday &gt; Date.today.years_ago( age )
    age
  end
</pre>
<p>check <a href="http://pastie.caboo.se/165173" rel="nofollow">http://pastie.caboo.se/165173</a> for the belonging tests</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://jonathanng.com/ruby-on-rails/calculate-age-in-rails-with-a-birthday/comment-page-1/#comment-54</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Fri, 29 Feb 2008 05:33:34 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanng.com/2008/01/28/calculate-age-in-rails-with-a-birthday/#comment-54</guid>
		<description>i also was not aware of all the gotchas involved with dates, times and timezones...

somehow i missed this detail:
you cannot compare DateTime with Time, so use the to_date Method on both Objects

def age
&#160;&#160;now = Time.now.utc.to_date
&#160;&#160;now.year - birthday.year - (birthday.to_date.change(:year =&gt; now.year) &gt; now ? 1 : 0)
end</description>
		<content:encoded><![CDATA[<p>i also was not aware of all the gotchas involved with dates, times and timezones&#8230;</p>
<p>somehow i missed this detail:<br />
you cannot compare DateTime with Time, so use the to_date Method on both Objects</p>
<p>def age<br />
&nbsp;&nbsp;now = Time.now.utc.to_date<br />
&nbsp;&nbsp;now.year &#8211; birthday.year &#8211; (birthday.to_date.change(:year =&gt; now.year) &gt; now ? 1 : 0)<br />
end</p>
]]></content:encoded>
	</item>
</channel>
</rss>
