Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Small method to calculate since time
Message
De
05/10/2011 06:47:46
 
 
À
05/10/2011 06:31:19
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01525501
Message ID:
01525637
Vues:
65
>>>>>>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 :-}
>>
>>Sorry to have killed your idea - but a DateTime() is just a date and a time. It gives a point in time
>>
>>>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.....
>>
>>>But I wonder whether just working with the Timespan would be better.
>>Not in this context.
>
>I'm not so sure. As above it may depend on what exactly Michel is trying to achieve?


Message# 1525535 posted 2011-10-04


Also, if you try that version, you will see it will give 5, 3 and 1:


Dim earlierDate As New DateTime(2007, 8, 5, 10, 15, 0)
Dim timespan As TimeSpan = DateTime.Now.Subtract(earlierDate)
Dim difference As New DateTime(timespan.Ticks)
MessageBox.Show(difference.Year.ToString + " " + difference.Month.ToString + " " + difference.Day.ToString)
We still have only one month completed since August 5th and it gives 3. So, I am not sure this approach will work as well.


Delta 2007-08-05 to 2011-10-04 is
4 years
1 month (2011-10-05 would be two)
and a number of days - too lazy

He says: We still have only one month completed since August 5th and it gives 3

So, how would a timespan fit in - given that the biggest unit a timespan knows about is a Day - no months, no years
http://msdn.microsoft.com/en-us/library/system.timespan.aspx

Also, a timespan doesn't have a starting point - ie a starting DateTime
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform