.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Code Snippets >> DateTime >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Calculate Age of user as of current Date

Posted By:Amit Mehra       Posted Date: November 19, 2008    Points: 10    Category: DateTime    URL: http://www.dotnetspark.com  

Below function will calculate the age of the user as of Current date (today).This fuction is written in C#/vb.net.  This function will return you the Age of the user in integer.

public static int GetAgeOfUser(DateTime DOB)

DateTime todaysDateTime = DateTime.Today; //Get Today DateTime
int noOfYears = todaysDateTime.Year - DOB.Year;
if (
DateTime.Now.Month < DOB.Month || (DateTime.Now.Month == DOB.Month && DateTime.Now.Day < DOB.Day))
return noOfYears;


in VB.NET Code Example

Public Shared Function GetAgeOfUser(ByVal DOB As DateTime) As Integer

Dim todaysDateTime As DateTime = DateTime.Today
'Get Today DateTime
Dim noOfYears As Integer = todaysDateTime.Year - DOB.Year
If DateTime.Now.Month < DOB.Month OrElse (DateTime.Now.Month = DOB.Month AndAlso DateTime.Now.Day < DOB.Day) Then
    noOfYears -= 1
End If
Return noOfYears

End Function


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend