Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Small method to calculate since time
Message
From
06/10/2011 13:25:54
 
 
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:
01525775
Views:
31
>>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
>
>OK - we need a DateTimeSpan :-}
   public struct DateTimeSpan
>    {
>        public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds)
>        {
>            Years = years; Months = months; Days = days;
>            Hours = hours; Minutes = minutes; Seconds = seconds;
>        }
>
>        public int Years;
>        public int Months;
>        public int Days;
>        public int Hours;
>        public int Minutes;
>        public int Seconds;
>
>        public override string ToString()
>        {
>            return string.Format("Years:{0} Months:{1} Days:{2} Hours:{3} Minutes:{4} Seconds:{5}",
>                Years, Months, Days, Hours, Minutes, Seconds);
>        }
>    }
I tried this:
static DateTimeSpan GetDateDiff(DateTime startDate, DateTime endDate)
>        {
>            int year = endDate.Year;
>            int month = endDate.Month;
>
>            TimeSpan startSpan = new TimeSpan(startDate.Day, startDate.Hour, startDate.Minute, startDate.Second);
>            TimeSpan endSpan = new TimeSpan(endDate.Day, endDate.Hour, endDate.Minute, endDate.Second);
>
>            if (endSpan - startSpan < new TimeSpan(0))
>            {
>                month -= 1;
>                if (month == 0)
>                {
>                    month = 12;
>                    year -= 1;
>                }
>            }
>            TimeSpan ts = endDate.Subtract(new DateTime(year, month, startDate.Day, startDate.Hour, startDate.Minute, startDate.Second));
>            return new DateTimeSpan(year - startDate.Year, month - startDate.Month, ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
>        }
Test:
DateTime earlierDate = new DateTime(2006, 1, 12,23, 15, 12);
>DateTime laterDate = new DateTime(2010, 1, 4, 9, 55, 40);
>
>            for (int i = 0; i < 1000; i++)
>            {
>                DateTimeSpan dts = GetDateDiff(earlierDate, laterDate);
>                Console.WriteLine("From:{0} To:{1}", earlierDate, laterDate);
>                Console.WriteLine(dts);
>
>                if (earlierDate.AddYears(dts.Years).AddMonths(dts.Months).AddDays(dts.Days)
>                    .AddHours(dts.Hours).AddMinutes(dts.Minutes).AddSeconds(dts.Seconds) != laterDate)
>                    throw new Exception("Crap code");
>
>                laterDate = laterDate.Subtract(new TimeSpan(1, 20, 10));
>            }
Maybe could be simplified more but at least GetDateDiff() is a bit shorter...


Currently busy with fairly complex xbrl stuff ...

Will have a look at it when time permits - hopefully during the weekend

Hope to find a bug or at least something better ;)
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform