Guest User

Untitled

a guest
Apr 26th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. public static int GetAges(DateTime d1, DateTime d2)
  2. {
  3. return new DateTime(d2.Subtract(d1).Ticks).Year;
  4. }
  5.  
  6. public static int GetAge(DateTime d1, DateTime d2)
  7. {
  8. return (int)((d2.Subtract(d1).Days / 365.25) + 0.001);
  9. }
  10.  
  11. class Program
  12. {
  13. public static int GetAge(DateTime d1, DateTime d2)
  14. {
  15. return d2.Year - d1.Year - 1 +
  16. ((d2.Month > d1.Month || d2.Month == d1.Month && d2.Day >= d1.Day) ? 1 : 0);
  17. }
  18.  
  19. public static int GetAges(DateTime d1, DateTime d2)
  20. {
  21. return (int)((d2.Subtract(d1).Days / 365.25) + 0.001);
  22. }
  23.  
  24. static void Main(string[] args)
  25. {
  26. DateTime d1 = DateTime.Parse("01.01.2017 12:00:00");
  27. DateTime d2 = DateTime.Parse("01.01.2018 11:59:00");
  28. Console.WriteLine(GetAge(d1, d2));
  29. Console.WriteLine(GetAges(d1,d2));
  30. Console.ReadKey();
  31. }
  32. }
Add Comment
Please, Sign In to add comment