Quantcast
Viewing all articles
Browse latest Browse all 46

Answer by Morgs for Difference in months between two dates

Here is my contribution to get difference in Months that I've found to be accurate:

namespace System{     public static class DateTimeExtensions     {         public static Int32 DiffMonths( this DateTime start, DateTime end )         {             Int32 months = 0;             DateTime tmp = start;             while ( tmp < end )             {                 months++;                 tmp = tmp.AddMonths( 1 );             }             return months;        }    }}

Usage:

Int32 months = DateTime.Now.DiffMonths( DateTime.Now.AddYears( 5 ) );

You can create another method called DiffYears and apply exactly the same logic as above and AddYears instead of AddMonths in the while loop.


Viewing all articles
Browse latest Browse all 46

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>