Guest User

Untitled

a guest
Apr 26th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. public static int GetAge(DateTime d1, DateTime d2)
  2. {
  3. var r = d2.Year - d1.Year;
  4. return d1.AddYears(r) <= d2 ? r : r -1;
  5. }
  6.  
  7. class Program
  8. {
  9. public static int GetAge1(DateTime d1, DateTime d2)
  10. {
  11. return d2.Year - d1.Year - 1 +
  12. ((d2.Month > d1.Month || d2.Month == d1.Month && d2.Day >= d1.Day) ? 1 : 0);
  13. }
  14.  
  15. public static int GetAge2(DateTime d1, DateTime d2)
  16. {
  17. var r = d2.Year - d1.Year;
  18. return d1.AddYears(r) <= d2 ? r : r -1;
  19. }
  20.  
  21. static void Main(string[] args)
  22. {
  23. DateTime d1 = DateTime.Parse("01.01.1997 12:00:00");
  24. DateTime d2 = DateTime.Parse("01.01.2000 12:00:00");
  25. Console.WriteLine($"method 1: Age = {GetAge1(d1, d2)}");
  26. Console.WriteLine($"method 2: Age = {GetAge2(d1, d2)}");
  27. Console.ReadKey();
  28. }
  29. }
Add Comment
Please, Sign In to add comment