Posted By

nvesic on 08/20/10


Tagged

DateTime difference days YEARS months


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

nvesic
Tyster


DateTime difference 2


 / Published in: C#
 

Datetime difference in years, months and days, corrected version of existing snippet called DateTime Difference provided by hnielsen

  1. class DateTimeDiff
  2. {
  3.  
  4. /// <summary>
  5. /// Difference btweeen dates expressed in years, months and days.
  6. /// </summary>
  7. /// <param name="dtStartDate"></param>
  8. /// <param name="dtEndDate"></param>
  9. /// <param name="YearsDiff"></param>
  10. /// <param name="MonthsDiff"></param>
  11. /// <param name="DaysDiff"></param>
  12. public static void TimeSpanToDate(
  13. DateTime dtStartDate,
  14. DateTime dtEndDate,
  15. out Int32 YearsDiff,
  16. out Int32 MonthsDiff,
  17. out Int32 DaysDiff
  18. )
  19. {
  20. bool bRegularOrder = true; // is the start date before end date
  21.  
  22. if (dtEndDate < dtStartDate) {
  23. // if the order of dates is reversed, dates are reversed, then difference is computed
  24. // however, we remember that order is reversed, so at the end we put minus before all results componenet
  25. DateTime d3 = dtStartDate;
  26. dtStartDate = dtEndDate;
  27. dtEndDate = d3;
  28. bRegularOrder = false;
  29. }
  30.  
  31. // compute difference in total months
  32. MonthsDiff = 12 * (dtEndDate.Year - dtStartDate.Year) + (dtEndDate.Month - dtStartDate.Month);
  33.  
  34. // based upon the days
  35. // adjust months & compute actual days difference
  36. if (dtEndDate.Day < dtStartDate.Day) {
  37. // check if both dates are the last day in the month
  38. if (dtStartDate.Day == DateTime.DaysInMonth(dtStartDate.Year, dtStartDate.Month)
  39. && dtEndDate.Day == DateTime.DaysInMonth(dtEndDate.Year, dtEndDate.Month)) {
  40. DaysDiff = 0; // whereas month number remains unchanged
  41. }
  42. else {
  43. MonthsDiff--;
  44. Int32 iBrojDanaPrethodnogMesecaZavrsnogDatuma = DayInMonthOfPreviousMonth(dtEndDate);
  45. DaysDiff = Math.Max(0, iBrojDanaPrethodnogMesecaZavrsnogDatuma - dtStartDate.Day)
  46. + dtEndDate.Day;
  47. }
  48. }
  49. else {
  50. DaysDiff = dtEndDate.Day - dtStartDate.Day;
  51. }
  52. // compute YearsDiff & actual MonthsDiff
  53. YearsDiff = MonthsDiff / 12;
  54. MonthsDiff -= YearsDiff * 12;
  55.  
  56. // if the order was reversed, put minus sign before all componenet results
  57. if (!bRegularOrder) {
  58. YearsDiff = -YearsDiff;
  59. MonthsDiff = -MonthsDiff;
  60. DaysDiff = -DaysDiff;
  61. }
  62. }
  63.  
  64. private static Int32 DayInMonthOfPreviousMonth(DateTime dt)
  65. {
  66. Int32 month = dt.Month;
  67. Int32 year = dt.Year;
  68.  
  69. // previous month
  70. month--;
  71. if (month == 0) {
  72. month = 12;
  73. year--;
  74. }
  75. return DateTime.DaysInMonth(year, month);
  76. }
  77.  
  78.  
  79.  
  80. }

Report this snippet  

You need to login to post a comment.