A Real Timespan Object With .Years & .Months A Real Timespan Object With .Years & .Months asp.net asp.net

A Real Timespan Object With .Years & .Months


Here's how to add some extension methods for this with C# using mean values:

public static class TimeSpanExtensions{    public static int GetYears(this TimeSpan timespan)    {        return (int)(timespan.Days/365.2425);    }    public static int GetMonths(this TimeSpan timespan)    {        return (int)(timespan.Days/30.436875);    }}


What you are looking for is indeed not what TimeSpan represents. TimeSpan represents an interval as a count of ticks, without respect to a base DateTime or Calendar.

A new DateDifference type might make more sense here, with a constructor or factory method taking a base DateTime, a target DateTime, and optionally a Calendar (defaulting to CultureInfo.CurrentCulture) with which to compute the various difference components (years, months, etc.)

EDIT: It looks to me like Noda Time may have the tools you need for this — the Period class "[r]epresents a period of time expressed in human chronological terms: hours, days, weeks, months and so on", and in particular Period.Between(then, now, PeriodUnits.AllUnits) seems to be the precise calculation you're asking for — but it's necessarily a much more complex class than TimeSpan. The Key Concepts page on the Noda Time wiki explains how "humans make time messy":

Leaving aside the tricky bits of astronomy and relativity, mankind has still made time hard to negotiate. If we all used ticks from the Unix epoch to talk about time, there wouldn't be a need for a library like Noda Time.

But no, we like to talk in years, months, days, weeks - and for some reason we like 12pm (which confusingly comes before 1pm) to be roughly the time at which the sun is highest... so we have time zones.

Not only that, but we don't all agree on how many months there are. Different civilizations have come up with different ways of splitting up the year, and different numbers for the years to start with. These are calendar systems.


Well, better late then nothing I suppose ;)

C# function giving everything

And this is my modified version :

private string GetElapsedTime(DateTime from_date, DateTime to_date) {int years;int months;int days;int hours;int minutes;int seconds;int milliseconds;//------------------// Handle the years.//------------------years = to_date.Year - from_date.Year;//------------------------// See if we went too far.//------------------------DateTime test_date = from_date.AddMonths(12 * years);if (test_date > to_date){    years--;    test_date = from_date.AddMonths(12 * years);}//--------------------------------// Add months until we go too far.//--------------------------------months = 0;while (test_date <= to_date){    months++;    test_date = from_date.AddMonths(12 * years + months);}months--;//------------------------------------------------------------------// Subtract to see how many more days, hours, minutes, etc. we need.//------------------------------------------------------------------from_date = from_date.AddMonths(12 * years + months);TimeSpan remainder = to_date - from_date;days = remainder.Days;hours = remainder.Hours;minutes = remainder.Minutes;seconds = remainder.Seconds;milliseconds = remainder.Milliseconds;return (years > 0 ? years.ToString() + " years " : "") +       (months > 0 ? months.ToString() + " months " : "") +       (days > 0 ? days.ToString() + " days " : "") +       (hours > 0 ? hours.ToString() + " hours " : "") +       (minutes > 0 ? minutes.ToString() + " minutes " : "");}