Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2005, 5:13 PM   #1
Ghost
Man Bear Pig Hunter
 
Ghost's Avatar
 
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4 Ghost is on a distinguished road
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 5:32 PM.
Ghost is offline   Reply With Quote
Old Dec 8th, 2005, 9:42 PM   #2
juliocarlosmg
Unverified User
 
juliocarlosmg's Avatar
 
Join Date: Nov 2004
Location: caribe
Posts: 5
Rep Power: 0 juliocarlosmg is on a distinguished road
Lightbulb solved(i think)

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;
}
juliocarlosmg is offline   Reply With Quote
Old Dec 9th, 2005, 12:33 AM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
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).
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC