473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
4 7630
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dhnriverside | last post by:
Hi I have a date in UK Format (dd/mm/yyyy). I want to get a series of days after that date, but NOT Saturday or Sunday For example, if the date is 13/12/2004 (next Monday) and I get the next 9...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
2
by: bufbec | last post by:
I have worked on this for hours and can't come up with a solution. Hope someone can help me. I have a table called TMBS_HMAUDIT_PARMS. this table contains data to tell me how often a person is...
3
by: Jason Huang | last post by:
Hi, In our C# Windows Form application, we are using the SQL Server 2000 as the database server. The Database table MyTable has a field RegistrationDate which represents the Date a client comes...
2
by: kath | last post by:
Hi, the following shows the contents of "datebook.xls" Date 8/9/2006 8/9/2006 8/9/2006 8/9/2006 8/9/2006
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
6
by: krishnakant Mane | last post by:
hello, I am strangely confused with a date calculation problem. the point is that I want to calculate difference in two dates in days. there are two aspects to this problem. firstly, I can't get...
4
by: Michael Sharman | last post by:
Hi guys, I'm a little confused with dates. Ok, all I want to do is store a date in MySQL as a datetime object and be able to read and format it using PHP. My datatype in MySQL is DATETIME and...
1
by: bharathreddy | last post by:
This article will explain how we can get the count of weekdays in between two dates. This will be usefull if we want to count the number of working days between two dates. Example: -------------...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.