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

R

Russell

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
 
N

nevin

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
 
P

phoenix

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
 
C

Charles Conlow

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top