Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TimeSpan not have a Years property?

I was writing a converter that takes a person's date of birth and produces their age in years. I wrote something that looked like this:

public class DateOfBirthToAgeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var date = value as DateTime?;
        if (date == null) return null;
        return (DateTime.Now - date).Years;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I found that there is no Years property on the TimeSpan that results from the subtraction of two DateTime objects. I was somewhat surprised by this. I thought about why there might not be a Years. I figured that it might be because of the leap day, but by that logic, there shouldn't be Days because of daylight savings.

The absence of Months made sense, since there is no standard month length.

I was able to write some different code to get the correct age, but I still really want to know why there is no Years or Weeks property on TimeSpan. Does anyone know the reason?

like image 828
Owen Johnson Avatar asked May 05 '15 17:05

Owen Johnson


People also ask

What is the default value for TimeSpan in C#?

Current Selected Time Interval Time(TimeSpan?): Defines the current selection of time interval. The default value is null.

How do you declare a TimeSpan?

The following example initializes a TimeSpan value to a specified number of hours, minutes, and seconds. TimeSpan interval = new TimeSpan(2, 14, 18); Console. WriteLine(interval. ToString()); // Displays "02:14:18".

How does TimeSpan work C#?

C# TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. C# TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.

How do you make a TimeSpan in C#?

The following example initializes a TimeSpan value to a specified number of hours, minutes, and seconds. TimeSpan interval = new TimeSpan(2, 14, 18); Console. WriteLine(interval. ToString()); // Displays "02:14:18".


2 Answers

A TimeSpan only contains the difference between two DateTime values. It is unknown which year this TimeSpan is in. That's also why it doesn't have a Months property.

Example:

TimeSpan.FromDays(60)

How many months are that? 1 or 2?


The absence of Months made sense since there is no standard month length.

There is no standard year length either because of leap years.

Workaround: If you really want to display an approximate value, then doing TimeSpan.TotalDays / 365.2425 will do just fine.

Edit: But only for rough estimations and not for birthdays. In birthday calculation, leap days will accumulate every 4 years as pointed out by Henk Holterman in the comments. Take a look here for calculation of birthdays.

like image 94
bytecode77 Avatar answered Oct 19 '22 06:10

bytecode77


Programmer's life is really hard.

The length of year is variable. Some years have 365 days and some have 366 days. According to the calendar, some years could even have missing days. If talking about culture it becomes more difficult since Chinese lunar calendar can have 13 months a year.

The length of month is variable, and this is well-known. This is also to know that in other calendars things can get worse.

The length of day is variable, because of daylight savings and this is not just culture dependent but also geography dependent.

The length of hour and minute are variable, because of leap seconds.

It seems the only thing that is reliable is the length of a second. So internally, timespan is stored in seconds (or milliseconds, which is the same).

But the variability of time units makes the answer "how many (years/months/days/hours/minites) for n seconds?" being always inaccurate.

This is why the developers end up with a solution that is useful in practical but not precise. They simply ignore daylight savings and leap seconds. However, since people hardly ask about years and months, they just decided not to answer those questions.

like image 23
Earth Engine Avatar answered Oct 19 '22 07:10

Earth Engine