A Real Timespan Object With .Years & .Months

后端 未结 8 2119
执念已碎
执念已碎 2020-12-05 02:51

Consider the following 2 scenarios: Scenario 1). Today is May 1st 2012, and Scenario 2). Today is September 1st 2012.

Now, consider that we write on our webpage the

8条回答
  •  孤城傲影
    2020-12-05 03:29

    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);
        }
    }
    

提交回复
热议问题