Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int GetAge(DateTime d1, DateTime d2)
- {
- var r = d2.Year - d1.Year;
- return d1.AddYears(r) <= d2 ? r : r -1;
- }
- class Program
- {
- public static int GetAge1(DateTime d1, DateTime d2)
- {
- return d2.Year - d1.Year - 1 +
- ((d2.Month > d1.Month || d2.Month == d1.Month && d2.Day >= d1.Day) ? 1 : 0);
- }
- public static int GetAge2(DateTime d1, DateTime d2)
- {
- var r = d2.Year - d1.Year;
- return d1.AddYears(r) <= d2 ? r : r -1;
- }
- static void Main(string[] args)
- {
- DateTime d1 = DateTime.Parse("01.01.1997 12:00:00");
- DateTime d2 = DateTime.Parse("01.01.2000 12:00:00");
- Console.WriteLine($"method 1: Age = {GetAge1(d1, d2)}");
- Console.WriteLine($"method 2: Age = {GetAge2(d1, d2)}");
- Console.ReadKey();
- }
- }
Add Comment
Please, Sign In to add comment