![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4
![]() |
Counting the Days....
Hey, I was wondering if one of you wiser people could help me with a little problem I'm having. I need to be able to count the days that are between any two given dates, here is what I had, and it's wrong.
TimeSpan daysApart = PopupCalendarEndDate.SelectedDate.Subtract(PopupCalendarStartDate.SelectedDate); Thanks a lot. Figured all of it out. --Closed-- Last edited by Ghost; Nov 30th, 2005 at 4:32 PM. |
|
|
|
|
|
#2 |
|
Unverified User
Join Date: Nov 2004
Location: caribe
Posts: 5
Rep Power: 0
![]() |
a couple of months ago i had to make a Date class for the school. here you have something that may help you. perhaps you will need to change something but the idea is correct and its work. it's very simple, so, don't need any explanations.
int CountLeapYears(int startingYear, int endingYear) { if (startingYear < 1 || startingYear > 9999 || endingYear < 1 || endingYear > 9999) throw new ArgumentOutOfRangeException(); if (startingYear == endingYear) throw new ArgumentException(); int ydiff = startingYear - endingYear; if (ydiff < 0) ydiff = -ydiff; return ydiff / 4 - ydiff / 100 + ydiff / 400; } int CountDays(DateTime dt1, DateTime dt2) { if (dt1 == null || dt2 == null) throw new ArgumentNullException(); if (dt1.Year >= dt2.Year) throw new ArgumentException(); int ydiff = dt2.Year - dt1.Year; int c = ydiff * 365 + CountLeapYears(dt1.Year, dt2.Year) - dt1.DayOfYear + dt2.DayOfYear; } |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
juliocarlosmg: I think the big -- Closed -- text was meant to indicate that the OP didn't need any more answers.
In addition to this your CountDays function does not work. If you give it two dates with the year value less than 4 apart, it will never think there is any leap years in between them. E.g. 2000/01/01 - 2001/01/01 I don't think you can use the difference between two years to determine the number of leap years, because once you subtract one from the other, you lose the information about whether either year was a special case (i.e divisible by 100 or 400). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|