Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Small method to calculate since time
Message
From
05/10/2011 04:13:35
 
 
To
04/10/2011 10:18:24
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01525501
Message ID:
01525627
Views:
37
>>>Yes but your start time in this example is 2007 ... 3 years ago...
>>
>>Yes, and based on those numbers, it doesn't make any sense that we would have five years and 3 months completed since August 5th.
>>
>>Do you have any idea why?
>
>That is due to the nature of a DateTime struct
>
>with a new DateTime(0), ie 0 ticks, the year = 1, month is 1 and day is one. iow it's a date and not a difference
>
>You may get away with subtracting one from year, month, and day, but I don't like it, for the following reasons
>
>(1) Leapyears:
>
>DateTime(2013, 01, 1, 00, 00, 0) - DateTime(2012, 01, 1, 00, 00, 0)
>
>gives that extra day. Converting that timespan to a Datetime() yields years = 2, months = 1 and days = 2
>
>but
>
>DateTime(2012, 01, 1, 00, 00, 0) - DateTime(2011, 01, 1, 00, 00, 0)
>
> yields years = 2, months = 1 and days = 1
>
>
>(2) months shorter than 31 days
>
>DateTime(2011, 02, 1, 00, 00, 0) - DateTime(2011, 01, 1, 00, 00, 0)
>
>
>yields years = 1, months = 2 and days = 1
>
>
>
>
>DateTime(2011, 03, 1, 00, 00, 0) - DateTime(2011, 02, 1, 00, 00, 0)
>
>
>yields years = 1, months = 1 and days = 29
>
>In other words, you are adding a timeSpan to DateTime(01, 01, 1, 00, 00, 0)
>A timespan is a number of ticks, or say seconds
>
>The first month of year 0001 has 31 days for a complete months. But the timespan is only a timespan ,ie seconds elapsed
>
>
>
>
>Also, the number of days in the months of year 0001 is 31, 28, 31, 30, 31 etc
>Who says that your timespan is following the same sequence ( say 2011 may 1 - 2011 april 1 is 30 days AND a full month)

Doh. Nice little bit of drive by coding (five minutes to spare before interviewing and no check online) and you have to blow it out of the water. I didn't even know that new DateTime(new TimeSpan(0).Ticks) would result in 01/01/01 :-}

But I wonder whether just working with the Timespan would be better. For example an 'accurate' calculation for two dates that were 90 days apart might give a result of 2 months 29 days, 3 months exactly or 3 months and 1 day depending on which three months fell into the span. Calling a timespan of 90 days three different things is, to me, just confusing.....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform