Return to Snippet

Revision: 18783
at October 8, 2009 05:48 by hnielsen


Updated Code
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;
}

Revision: 18782
at October 8, 2009 05:48 by hnielsen


Initial Code
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;
        }

Initial URL

                                

Initial Description
Calculates the years, months and days between two DateTime objects

Initial Title
DateTime difference

Initial Tags

                                

Initial Language
C#