(35S) years, months and days between two dates
|
04-25-2016, 07:56 PM
(This post was last modified: 06-15-2017 01:33 PM by Gene.)
Post: #1
|
|||
|
|||
(35S) years, months and days between two dates
Some time ago there were several discussions on calculating the number of years, months and days between two given dates. Indeed these figures are sometimes more handy than the pure number of days as returned by the ΔDAYS command that's found on some calculators (or other calendar software).
However, there are many cases where there are two or even three possible valid solutions for the same input dates, depending on the way the number of days is evaluated. This has been discussed in other threads, so there is no need to repeat it here again. ;-) The following program uses a quite straightforward method: it counts the number of days until the end of the first month, then determines the number of months and years until the second month, and finally adds the number of days in the latter. Example: date1: 25 February 1999 date2: 13 November 2005 That's 4 days until 1 Mar 1999, then 6 years and 8 months until 1 Nov 2005, and finally 12 more days, i.e. 6 years, 8 months and 16 days altogether. And that's what the program returns. The program employs two formulas that may be of interest for other calendar programs as well. Both are related to the number of days in a given month. The number of days in month m is evaluated using the following formula: For all months except February: ndays = 30 + (m + m div 8) mod 2 Here div stands for integer division. If your calculator does not feature a command like INT÷ or IDIV, simply use a regular division followed by INT resp. IP. For February the program has to determine if the year y is a leap year or not. This is done by checking whether y can be divided by 4 resp. 100 resp. 400 and summing up the number of cases where this is not (!) possible. For instance, 1976 can be divided by 4, but not by 100 or 400, so two out of three conditions do not apply. Numerically this is done by adding the signs of y mod {4, 100, 400}: n = sgn(y mod 4) + sgn(y mod 100) + sgn(y mod 400) Now every possible result for n (0...3) stands for a unique combination of the three conditions that determine whether a year is leap or not: n = 0: all three conditions apply => leap year n = 1: one condition is false. This must be the division by 400 => common year n = 2: two conditions are false. These must be the divisions by 400 and 100 => leap year n = 3: all three conditions do not apply => common year So if n is odd, y is a common year, and if n is even, it's a leap year. For February this means ndays = 29 – [sgn(y mod 4) + sgn(y mod 100) + sgn(y mod 400)] mod 2 The program implements a variation of this formula: instead of the mod operator it NANDs n with 1, which results in -1 if n is even and -2 if n is odd. Adding 30 yields the number of days in February: ndays = 30 + [sgn(y mod 4) + sgn(y mod 100) + sgn(y mod 400)] NAND 1 Please note that the latter method may not work on calculators with a different NAND implementation. Finally, here is the listing: Code: Y001 LBL Y And here's how to use it: Enter the earlier date and the later date, separated by [ENTER]. Important: use the dd.mmyyyy format. Code: date1 [ENTER] date2 Example: How many years, months and days have elapsed between 25 February 1999 and 13 November 2005? Code: 25.021999 [ENTER] 13.112005 That's 6 years, 8 months and 16 days. These three values are also returned in Y, Z and T as well as in the variables Y, M and D. As usual: this program may contain errors, so please do your own tests. I appreciate all comments and corrections. Dieter Edit: corrected an error in line Y060 (had wrong jump target). |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)