//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Date/Time Routines :: Given two dates, how do I determine an age?


Given two dates, how do I determine an age?

There are a few considerations here. Most people funnel into big mathematical equations, dividing the datediff in days by 365.333333333 and doing all kinds of logic to equate that to an age. In addition, we need to be wary of leap years and treat those cases differently. While a leap year baby's true age might technically be in the single digits, the more important piece of data (usually) is that x number of years have passed since they were born. Here is an example in VBScript: 
 
<% 
    ' use DateSerial(y,m,d) to avoid locale issues 
    date1 = DateSerial(1974,2,24) 
    date2 = DateSerial(year(date), month(date), day(date)) 
 
    ' make sure we have a valid date! 
    if date2 >= date1 then 
 
        ' determine if leapYearBaby 
        if month(date1) = 2 and day(date1) = 29 then 
            leapBaby = true 
        end if      
 
        ' get absolute number of years 
        ageInYears = cint(datediff("YYYY", date1, date2)) 
 
        ' get date1's month and day in terms of date2's year 
        date1alt = dateadd("yyyy", ageInYears, date1) 
 
 
        if date1alt > date2 then 
            ' their birthday hasn't hit yet in date2's year 
            ageInYears = ageInYears - 1 
        end if 
 
        if leapBaby = true then 
            ' need to format output slightly 
            yearsPassed = ageInYears 
            ageInYears = ageInYears \ 4 
        end if 
 
        response.write "Age: " & ageInYears 
        if leapBaby then response.write " (" & yearsPassed & " years since birth)" 
    else 
        response.write "Invalid date." 
    end if 
%>
 
And here is an example in T-SQL, which also accounts for leap year birthdays (and thanks to Steve Kass for correcting my earlier script): 
 
DECLARE @birthdate SMALLDATETIME, @endDate SMALLDATETIME 
SET @birthdate = '19740224' 
SET @endDate = GETDATE() 
 
SELECT DATEDIFF 

    YEAR, 
    @birthdate, 
    @endDate 
) - CASE 
    WHEN 100 * MONTH(@endDate) + DAY(@endDate) 
    < 100 * MONTH(@birthdate) + DAY(@birthdate) 
    THEN 1 ELSE 0 END 
 
(You'll probably want to put this into a function, rather than calculating it inline, if you're using SQL Server 2000.) 
 
Finally, here is a solution in Access, given a table named DOBs with a column named dob (with only date, and no time information -- or time set to midnight): 
 
SELECT 
    dob,  
    DateDiff 
    ( 
        "yyyy", 
        CDate(dob), 
        Date() 
    ) 
    - IIF 
    ( 
        DateAdd 
        ( 
            "d", 
            Day(dob)-1, 
            DateAdd 
            ( 
                "m", 
                Month(dob)-1, 
                CDate(year(Date()) & "-01-01") 
            ) 
        ) 
        > date(), 
        1, 
        0 
    ) AS age 
FROM 
    dobs

Related Articles

Can I get millisecond accuracy in ASP?
Can I make VBScript format dates for me?
Could I get a little help with dates?
Given a date, how do I find the beginning and end of that week?
How do I calculate dates, such as the first day of the month?
How do I convert a DATEDIFF to days, hours, and minutes?
How do I convert a timespan, in seconds, to HH:MM:SS?
How do I convert local time to UTC (GMT) time?
How do I count the number of business days between two dates?
How do I delimit/format dates for database entry?
How do I determine the number of seconds since 1/1/1970?
How do I display time in military format?
How do I implement a calendar / datepicker in ASP?
How do I select time only from a DATETIME column?
Why do I have problems inserting NOW() into a database?
Why does JavaScript's document.lastModified() not work in ASP files?

 

 


Created: 1/7/2002 | Last Updated: 2/6/2004 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (363)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...