/ Published in: C#
                    
                                        
Calculates the years, months and days between two DateTime objects
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
public static void TimeSpanToDate(DateTime larger, DateTime smaller, out int years, out int months, out int days)
{
// we want larger to be the larger (newest) date
// flip if we need to
if (larger < smaller)
{
DateTime d3 = smaller;
smaller = larger;
larger = d3;
}
// compute difference in total months
months = 12 * (larger.Year - smaller.Year) + (larger.Month - smaller.Month);
// based upon the 'days',
// adjust months & compute actual days difference
if (larger.Day < smaller.Day)
{
months--;
days = DateTime.DaysInMonth(smaller.Year, smaller.Month) - smaller.Day + larger.Day;
}
else
{
days = larger.Day - smaller.Day;
}
// compute years & actual months
years = months / 12;
months -= years * 12;
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                