By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

FInding the days between 2 dates without using the inbuilt date functions or the DateTime Type

P: n/a
I have an assignment that I have to complete. I have to write a
windows app in C#. Here is the spec:

1/ Date Comparison
Build a program that will find the number of days between two dates.
You CANNOT use the inbuilt date functions or the DateTime Type. Your
program should work with any date between 1900 and 3000.

Here are your acceptance tests:

Days between 04/01/02 and 01/01/02 equals 3
Days between 01/05/1998 and 12/10/1977 equals 7506
Days between 01/01/2004 and 01/01/2003 equals 365
Days between 01/01/2002 and 01/01/2000 equals 731

I figure I could convert the dates to doubles and then subtract one
from the other but that wouldnt take leap years into account!!

Any ideas to get me started would be really appreciated!!

thanks
RuSs
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Russell,

I can give you a pointer to the number of days in a month for a given month
in a year taking leap years into account but I stress this is completely
untested and I'm fumbling trying to remember it from my pascal days (and
convert it at the same time). I guess using this you would need to get each
whole month and work out its number of days and then work out the remaining
days of any left over.

private bool isLeapYear(int year)
{
//needs some serious testing to make sure this works for every leap year
(esp y2k)
return (year % 4==0) &&((year % 100 !=0) | (year % 400 == 0));
}

private int DaysInAMonth(int month, int year)
{
int[] months = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31};
int x = months[month-1];
return (month == 2 && IsLeapYear(year))?++x:x;
}

Nev
"Russell" <ru************@hotmail.com> wrote in message
news:15*************************@posting.google.co m...
I have an assignment that I have to complete. I have to write a
windows app in C#. Here is the spec:

1/ Date Comparison
Build a program that will find the number of days between two dates.
You CANNOT use the inbuilt date functions or the DateTime Type. Your
program should work with any date between 1900 and 3000.

Here are your acceptance tests:

Days between 04/01/02 and 01/01/02 equals 3
Days between 01/05/1998 and 12/10/1977 equals 7506
Days between 01/01/2004 and 01/01/2003 equals 365
Days between 01/01/2002 and 01/01/2000 equals 731

I figure I could convert the dates to doubles and then subtract one
from the other but that wouldnt take leap years into account!!

Any ideas to get me started would be really appreciated!!

thanks
RuSs

Nov 16 '05 #2

P: n/a
Sounds like a homework to me.

Anyway, google for "julian date", it's a way to convert a date into a unique
long. Do that for both dates and substract them to get the number of days
between them.

HTH

Yves

"Russell" <ru************@hotmail.com> schreef in bericht
news:15*************************@posting.google.co m...
I have an assignment that I have to complete. I have to write a
windows app in C#. Here is the spec:

1/ Date Comparison
Build a program that will find the number of days between two dates.
You CANNOT use the inbuilt date functions or the DateTime Type. Your
program should work with any date between 1900 and 3000.

Here are your acceptance tests:

Days between 04/01/02 and 01/01/02 equals 3
Days between 01/05/1998 and 12/10/1977 equals 7506
Days between 01/01/2004 and 01/01/2003 equals 365
Days between 01/01/2002 and 01/01/2000 equals 731

I figure I could convert the dates to doubles and then subtract one
from the other but that wouldnt take leap years into account!!

Any ideas to get me started would be really appreciated!!

thanks
RuSs

Nov 16 '05 #3

P: n/a
Russell wrote:
Any ideas to get me started would be really appreciated!!


Will you be citing your sources when you hand this in?
Nov 16 '05 #4

P: n/a
Assuming:
Western European derived cultures

Given:
Every month in any year has a given number of days
except (some) februarys...
except some months ~bef. Julian Dating (see also Gregorian)
see also culture specific calendar dating

Given:
Every year has XXX days
except modulus year/4
except modulus year/100
except moulus year/400
...
Do:
[the hard way]
build tables for each year between and inclusive of
(STARTYEAR.MONTH.DAY) and (ENDYEAR>MONTH.DAY)
perform subtraction on the inclusives
you have your answer!

Do:
[the easier way]
julian.start = julian(STARTYEAR.MONTH.DAY) {implementation derivitive}
julian.end = julian(ENDYEAR.MONTH.DAY) {implementation derivitive}
juliandays = julian.end - julian.stary {implementation derivitive}
you have your answer!

Do:
[the easiest way]
Post to newsgroups and hope someone will just type in your assignment
for you!

Then:
The remainder is left as an excercise for the student... but I'M SURE you
knew that already :)

Charles

"Russell" <ru************@hotmail.com> wrote in message
news:15*************************@posting.google.co m...
I have an assignment that I have to complete. I have to write a
windows app in C#. Here is the spec:

1/ Date Comparison
Build a program that will find the number of days between two dates.
You CANNOT use the inbuilt date functions or the DateTime Type. Your
program should work with any date between 1900 and 3000.

Here are your acceptance tests:

Days between 04/01/02 and 01/01/02 equals 3
Days between 01/05/1998 and 12/10/1977 equals 7506
Days between 01/01/2004 and 01/01/2003 equals 365
Days between 01/01/2002 and 01/01/2000 equals 731

I figure I could convert the dates to doubles and then subtract one
from the other but that wouldnt take leap years into account!!

Any ideas to get me started would be really appreciated!!

thanks
RuSs

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.