---
title: "How to calculate age in c#?"  
description: "How to calculate age in c#?"  
author: "Anonymous User"  
published: 2014-10-26  
updated: 2014-10-27  
canonical: https://www.mindstick.com/forum/2436/how-to-calculate-age-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to calculate age in c#?

How to calculate [age](https://www.mindstick.com/articles/23262/the-modern-age-these-technology-trends-can-make-your-life-easier) from DOB\

```
 class Personal{ public DateTime DateOfBirth { get; set; } public string Age { get; set; } }        Personal per=new Personal();       Console.WriteLine("Enter the Date Of Birth :",per.DateOfBirth);         per.DateOfBirth = DateTime.Parse(Console.ReadLine()); public string GetAge(DateTime DateOfBirth)         {        }
```

How can i write for [function to calculate](https://www.mindstick.com/forum/158909/how-do-you-use-the-avg-function-to-calculate-the-average-value-of-a-column-in-sql) age from DOB and display\

```
       Console.WriteLine("The DOB is" +DateOfBirth);       Console.WriteLine("the Age is ...." +Age);DOB is 04/02/1994
```

i want display age is 19 years, 6 months, and 9 [days](https://www.mindstick.com/articles/157271/how-to-get-cloudways-free-trial-for-14-days-free)

## Replies

### Reply by Anonymous User

try this code\
\
**ex:**\
\

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace IfExample{  class DateTimeExtensions  {  public  string ToAgeString( DateTime dob)  {  DateTime dt = DateTime.Now;  int days = dt.Day - dob.Day;  if (days < 0)  {  dt = dt.AddMonths(-1);  days += DateTime.DaysInMonth(dt.Year, dt.Month);  }  int months = dt.Month - dob.Month;  if (months < 0)  {  dt = dt.AddYears(-1);  months += 12;  }  int years = dt.Year - dob.Year;  return string.Format("{0} year{1}, {2} month{3} and {4} day{5}",  years, (years == 1) ? "" : "s",  months, (months == 1) ? "" : "s",  days, (days == 1) ? "" : "s");  }  static void Main()  {  DateTimeExtensions obj = new DateTimeExtensions();  Console.WriteLine("Enter your date of birth in mm/dd/yyyy format to calculate age");  DateTime dob = DateTime.Parse(Console.ReadLine());  string s = obj.ToAgeString(dob);  Console.WriteLine(s);  Console.ReadLine();  }  }}
```


---

Original Source: https://www.mindstick.com/forum/2436/how-to-calculate-age-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
