Calculate Age in ASP.Net C#

Some time we face problem related to calculating exact age in year. In this blog I have explained how to calculate exact age in year in ASP.Net with C#.For demonstration, take one TextBox (TextBox1), one Button (Button1) and one Label (Label1) control on your ASPX page. Now generate Button’s Click event (Button1_Click ()) and write below line of code. 

// Get current date time
DateTime d = DateTime.Now;
// Changed MM/dd/yyyy format to dd/MM/yyyy fromat
string s = d.ToString('dd/MM/yyyy');
// Convert date time in string (s) to DateTime (Todate) data type
DateTime Todate = DateTime.Parse(s, CultureInfo.CreateSpecificCulture('en-IA'));
// Convert entered date string (TextBox1.Text) to DateTime (givenDate) Data Type
DateTime givenDate = DateTime.Parse(TextBox1.Text);
// Count total days
double days = Todate.Subtract(givenDate).Days;
// Convert days to year and display into Label.Text
Label1.Text = Math.Floor(days / 365.24219).ToString();


1.    If you want to used Global DateTime format then replay below line from

given line

DateTime d = DateTime.Now;


DateTime d = DateTime.UtcNow;

DateTime.UtcNow tells you the date and time as it would be in

Coordinated Universal Time, which is also called the Greenwich Mean Time

time zone

DateTime.Now gives the date and time as it would appear to someone in

your current locale.

2.      Here I have given DateTime culture is “en-IA”. Change culture

according your required.

DateTime Todate = DateTime.Parse(s, CultureInfo.CreateSpecificCulture('en-IA'));
Culture Description

en-AU (English Australia): 24/10/2011

en-IA (English India): 24-10-2011

en-ZA (English South Africa): 2011/10/24

en-US (English United States): 10/24/2011



Last updated:9/18/2014 1:24:10 PM


Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)


Leave Comment