Avatar ·

How to calculate a person's age from date of birth (DateTime) in C#?

Hello everyone! I'm a beginner in C# and writing a small application for client management. I need to automatically calculate a person's age based on their date of birth, which is stored as `DateTime`. The problem is that simply subtracting the current date from the date of birth and dividing by 365 doesn't work correctly. Leap years, different numbers of days in months — all of this introduces errors. I tried this code: ```csharp DateTime birthDate = new DateTime(2000, 2, 29); // February 29 DateTime today = DateTime.Today; int age = (today - birthDate).Days / 365; Console.WriteLine(age); // Sometimes outputs 23, but should be 24 ``` But for people born on February 29, or for those whose birthday hasn't occurred yet in the current year, the result is incorrect. How to correctly and reliably calculate