Knowing the number of days between two dates is frequently necessary for calculating interest, apportioning expenditures, or calculating average use of items. We are also interested in the more trivia type questions such as how many days before an important date, how many days between certain historical dates, and even how many days we have lived.
This program will enable you to determine the number of days between dates either for serious use or just curiosity. The program in Listing 1 is for the 8K ROM machine with 1K RAM. A 4K ROM adaptation is included in Listing 2.
Operating Instructions
1) Type in the program in Listing 1. Hit RUN and ENTER.
2) The prompts will ask the user to ENTER FIRST DATE. Do so in this order: Type in the month as a number from 1 to 12 and hit ENTER. Type in the day of the month and ENTER. Finally, type in the year giving all four digits and ENTER.
3) Repeat this process for the second date. The first date must be earlier than the second. On the 4K ROM the two dates cannot be more than 88 years apart, but a method is supplied for dealing with this problem.
4) The answer will then be given on the screen.
5) To repeat, hit ENTER. To quit, hit any number or letter and then ENTER.
Error Checking
The input routine does not allow the user to enter more than 12 for a month or 31 for a day. The user with enough memory may want to add a more detailed error checking routine that rejects nonexistent dates, e.g., June 31.
Modifications for the 4K ROM
Listing 2 gives the lines which must be altered to allow the program to operate properly on the 4K ROM (ZX80 or MicroAce).
4K ROM Problem: Dates more than 88 Days apart
The integer Basic of the 4K ROM ZX80 will not handle numbers greater than 32767. For this reason, the program will reject the second year if it is more than 88 years after the first. However, there is a method for finding the number of days between two dates more than 88 years apart. This method requires the use of one or more mid-date(s) which must be less than 89 years from the date next to it in the series. The number of days between the first date and the mid-date is computed and the results recorded. Then the number of days between the mid-date and the second date is computed and added manually to the first result.
As an example of how this method works, let’s calculate how many days apart July 7, 1881, and January 1, 1981 are.
ENTER FIRST DATE: 7/7/1881
ENTER SECOND DATE: 1/1/1921
(mid-date)
14423 DAYS APART
(ENTER)
ENTER FIRST DATE: 1/1/1921
(mid-date)
ENTER SECOND DATE: 1/1/1981
21915 DAYS APART
14423
+21915
------
36338 Days apart
Listing 1. Days between Two Dates (8K ROM).
20 DIM N(12)
25 LET N(1)=0
30 LET N(2)=31
35 LET N(3)=59
40 LET N(4)=90
45 LET N(5)=120
50 LET N(6)=151
55 LET N(7)=181
60 LET N(8)=212
65 LET N(9)=243
70 LET N(10)=273
75 LET N(11)=304
80 LET N(12)=334
85 LET D=0
90 PRINT " M/D/Y"
100 PRINT "ENTER FIRST DATE: ";
110 INPUT M1
112 IF M1>12 THEN GOTO 110
120 PRINT M1;"/";
130 INPUT D1
132 IF D1>31 THEN GOTO 130
140 PRINT D1;"/";
150 INPUT Y1
160 PRINT Y1
170 PRINT "ENTER SECOND DATE: ";
180 INPUT M2
185 IF M2>12 THEN GOTO 180
190 PRINT M2;"/";
200 INPUT D2
205 IF D2>31 THEN GOTO 200
210 PRINT D2;"/";
220 INPUT Y2
225 IF Y2-Y1>88 OR Y2<Y1 THEN GOTO 220
230 PRINT Y2
240 FOR X=Y1 TO Y2-1
250 LET D=D+365
255 IF INT (X/100)*100=X AND INT (X/400)*400<X THEN GOTO 265
260 IF INT (X/4)*4=X THEN LET D=D+1
265 NEXT X
270 IF Y1=Y2 THEN LET D=365-D
275 IF INT (Y1/100)*100=Y1 AND INT (Y1/400)*400<Y1 THEN GOTO 285
280 IF INT (Y1/4)*4=Y1 AND M1>2 THEN LET D=D-1
285 IF INT (Y2/100)*100=Y2 AND INT (Y2/400)*400<Y2 THEN GOTO 300
290 IF INT (Y2/4)*4=Y2 AND M2>2 THEN LET D=D+1
300 LET D=D-N(M1)-D1
310 LET D=D+N(M2)+D2
315 IF Y1=Y2 THEN LET D=D-365
320 PRINT D;" DAYS APART"
330 INPUT Y$
340 CLS
350 IF Y$="" THEN GOTO 20
Listing 2. 4K ROM Adaptations.
255 IF 100*(X/100)=X AND 400*(X/400)<X THEN GO TO 265
260 IF 4*(X/4)=X THEN LET D=D+1
270 IF Y1=Y2 THEN LET D=D-365
275 IF 100*(Y1/100)=Y1 AND 400*(Y1/400)<Y1 THEN GO TO 285
280 IF 4*(Y1/4)=Y1 AND M1>2 THEN LET D=D-1
285 IF 100*(Y2/100)=Y2 AND 400*(Y2/400)<Y2 THEN GO TO 300
290 IF 4*(Y2/4)=Y2 AND M2>2 THEN LET D=D+1
315 Delete
Variables Used
N(n) Days used in year by nth month
D Day counter (Number of days
between dates)
M1 Month of 1st date
D1 Day of month for 1st date
Y1 Year of 1st date
M2 Month of 2nd date
D2 Day of month for 2nd date
Y2 Year of 2nd date
X Temporary variable used
as counter
Routines
The routines are found at the following lines:
20-85: Initialization.
95-230: Input dates.
240-310: Count days between dates. Compensate for leap years.
320: Print results.
330-340: Input “”to repeat.
Examples
The American Civil War lasted from February 8, 1861, to May 26, 1865. How many days did it last?
M/D/Y ENTER FIRST DATE: 2/8/1861 ENTER SECOND DATE: 5/26/1865 1568 DAYS APART
John has a report due on July 20, 1982, and today is May 15, 1982. How many days does he have to complete the report?
M/D/Y ENTER FIRST DATE: 5/15/1982 ENTER SECOND DATE: 7/20/1982 56 DAYS APART
Herb was born on August 1, 1945. How many days old will he be on his 40th birthday?
M/D/Y ENTER FIRST DATE: 8/1/1945 ENTER SECOND DATE: 8/1/1985 14610 DAYS APART
M/D/Y
ENTER FIRST DATE: 1/1/1980
ENTER SECOND DATE: 4/1/1980
91 DAYS APART
M/D/Y
ENTER FIRST DATE: 1/1/1979
ENTER SECOND DATE: 4/1/1979
90 DAYS APART
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.