• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Difference without using Date functions  RSS feed

 
Sunny Nambiar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
This is the problem. I would be highly obliged if anyone could get me a sample code for this.
Regards
Sunny

Given two dates (during the years 1901 and 2999, inclusive), find the number of days between the two dates.
Input: Six sets of data. Each set has two dates in the format month, day, year. For example, the input line '6, 2, 1983, 6, 22, 1983' represents
June 2, 1983 to June 22, 1983.
Output: The number of days between the given dates, excluding the starting date and the ending date. For example, there are 19 days between June 2, 1983 and June 22, 1983; namely, June 3, 4, ..., 21.
Sample Input (3 sets):
Input Line #1: 6, 2, 1983, 6, 22, 1983
Output #1: 19
Input Line #2: 7, 4, 1984, 12, 25, 1984
Output #2: 173
Input Line #3: 1, 3, 1989, 3, 8, 1983
Output #3: 1979
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the GregorianCalendar class if you don't want to use Date. You can construct a GregorianCalendar by supplying the year, month and day. The class also has methods to convert the date to a long(the date in milliseconds). you should be able to figure out how to derive the day difference between two longs.
 
Sunny Nambiar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not supposed to use the built in classes like Date and Calendar or their methods.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you can do is read in all 6 input in an array A[].
The total days = ( A[5] - A[2] ) * 365 + ( A[4] - A[1] ) * 30 + A[3] - A[0] - 1
On top of this, you should consider leap year and month issues. The rule is:
Leap year: mode(year, 4)=0 && mode(year, 100)!=0 && mode(year, 400)=0 && ...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The total days = ( A[5] - A[2] ) * 365 + ( A[4] - A[1] ) * 30 + A[3] - A[0] - 1
Ummmm... well, if an approximation is what you want, then OK. (Think - where does that "30" come froml?) I doubt that's good enough for this asignment however. This is a school assignment, right Sunny? I mean, there's no good reason in the real world to disallow use of Date and Calendar. I assume the intent here is to get you thinking about how to write a working program to calculate something moderately complex. In this case, it's really something you shoud do yourself, to benefit from the excercise.
As a hint though - for a given date, try calculating how many days it is since Dec 31 of the previous year. This is called the Julian date. For example on Jan 1 the Julian date is 1; on Feb 1, it's 32. On March 1, it's 60 - unless it's a leap year, in which case it's 61. (You can ignore leap years at first whil eyou work out the rest of the logic, and then modify later to include leap years.) You can create an array of ints containing the number of days in each month, and use this to calculate the Julian date at the start of each month. Then add the day of the month to get the Julian date for a particular date. Once you can calculate a Julian date, then you can compare two dates by comparing their Julian dates - plus some extra logic if they are in different years (similar to what Don shows above).
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Liu:
The total days = ( A[5] - A[2] ) * 365 + ( A[4] - A[1] ) * 30 + A[3] - A[0] - 1
On top of this, you should consider leap year and month issues.

Julian date is a good idea though!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!