---
title: "How can I calculate someone's age based on a DateTime type birthday?"  
description: "How can I calculate someone's age based on a DateTime type birthday?"  
author: "Steilla Mitchel"  
published: 2023-07-12  
updated: 2023-07-13  
canonical: https://www.mindstick.com/forum/159046/how-can-i-calculate-someone-s-age-based-on-a-datetime-type-birthday  
category: "c#"  
tags: ["c#", "datetime"]  
reading_time: 2 minutes  

---

# How can I calculate someone's age based on a DateTime type birthday?

How can I calculate someone's [age](https://www.mindstick.com/articles/23262/the-modern-age-these-technology-trends-can-make-your-life-easier) based on a [DateTime](https://www.mindstick.com/forum/12949/how-to-validate-if-a-datetime-field-is-not-null-empty) type birthday?

## Replies

### Reply by Aryan Kumar

Sure, here is how you can calculate someone's age based on a `DateTime` type birthday in C#:

C#

```plaintext
DateTime today = DateTime.Now;
DateTime birthday = new DateTime(1990, 1, 1);

int age = (today.Year - birthday.Year);

// If today is before the birthday, then subtract 1 from the age.
if (today < birthday)
{
    age--;
}

Console.WriteLine("The person is {0} years old.", age);
```

This code first creates two `DateTime` objects: `today` and `birthday`. The `today` object represents the current date and time, and the `birthday` object represents the person's birthday.

The code then calculates the person's age by subtracting the `birthday` object from the `today` object. The `year` property of the `DateTime` object is used to get the year of the date.

The code then checks if the `today` object is before the `birthday` object. If it is, then the code subtracts 1 from the age. This is because the person is not yet born on the day before their birthday.

Finally, the code prints the person's age to the console.

Here is an explanation of the code:

- The `DateTime.Now` property is used to get the current date and time.
- The `new DateTime()` constructor is used to create a `DateTime` object with a specific date and time.
- The `Year` property is used to get the year of a `DateTime` object.
- The `-` operator is used to subtract two `DateTime` objects.
- The `if` statement is used to check if the `today` object is before the `birthday` object.
- The `Console.WriteLine()` method is used to print a string to the console.


---

Original Source: https://www.mindstick.com/forum/159046/how-can-i-calculate-someone-s-age-based-on-a-datetime-type-birthday

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
