Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateDiff

Status
Not open for further replies.

snowboardr

Programmer
Joined
Feb 22, 2002
Messages
1,401
Location
PH
I am wondering how to get the datediff on a birthday to return the current age, I think what its doing is returning the rounded up age, because the birthday i am using is aug 27, 1983 and its returning 23, and im 22.

Jason


Code:
ubirthday = DateDiff("yyyy",ubirthday,date()) & " /"
 
nevermind i figured it out


Code:
Function GetAge(date1,date2)
	Dim tmpage, leapBaby, ageInYears, date1alt
	leapBaby = false
' use DateSerial(y,m,d) to avoid locale issues 
    date1 = DateSerial(datepart("yyyy",date1),datepart("m",date1),datepart("d",date1)) 
    date2 = DateSerial(datepart("yyyy",date2),datepart("m",date2),datepart("d",date2)) 
 
    ' 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 
 
   	
        if leapBaby = false then 
			tmpage = ageInYears  
		Else  
			'leap baby
			tmpage = yearsPassed 
		End If
		
		GetAge = tmpage
    else 
		'bad date
    end if 


End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top