(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). |
|||
06-07-2016, 01:13 PM
Post: #2
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Thank you.
|
|||
06-08-2016, 10:07 AM
Post: #3
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Hi,
This remember me good discussions ! Thanks Dieter. Gérard. |
|||
06-13-2016, 04:37 AM
Post: #4
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Nicely done Dieter. Just a question. What if the number of days add up to more than 30? Say 2 Feb 2016 to August 31 2017. 27 days to end of February plus 30 days from start of 1 August to 30 August.
|
|||
06-14-2016, 09:31 PM
(This post was last modified: 06-14-2016 09:42 PM by Dieter.)
Post: #5
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
(06-13-2016 04:37 AM)Ender Wrote: Nicely done Dieter. Just a question. What if the number of days add up to more than 30? Say 2 Feb 2016 to August 31 2017. 27 days to end of February plus 30 days from start of 1 August to 30 August. Simple. In this case there is an unambiguous solution because day2 > day1. So the result is 1 year, 6 months and 29 days. I wrote the program "...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.". This rule is only applied where more than one valid result may exist, i.e. where day2 < day1. Sorry if I was not clear enough here. BTW, looking at the program again I found an error in line Y060 that is now corrected. ;-) Dieter |
|||
06-15-2016, 03:33 PM
Post: #6
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Hi,
Because I'm a bit misunderstanding the "NAND" command which is too implemented in the WP34s, may I ask for an explication ? Thanks. Gérard. |
|||
06-15-2016, 08:24 PM
Post: #7
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
(06-15-2016 03:33 PM)ggauny@live.fr Wrote: Because I'm a bit misunderstanding the "NAND" command which is too implemented in the WP34s, may I ask for an explication ? NAND is a logical operator, the inverse of AND (NAND = NOT AND). It behaves this way: 0 NAND 0 = 1 0 NAND 1 = 1 1 NAND 0 = 1 1 NAND 1 = 0 So the result of the NAND operation is 0 if both arguments are 1, and 0 else. The 35s does not use a logical NAND (which is what the 34s calculates in its regular real modes), but is uses a bitwise NAND. Here the two arguments are taken bit by bit, and the respective bit of the result is calculated according to the table above. Now an even number has 0 as its last bit. So NANDing it with 1 means (look at the table) that the last bit of the result is 1. All other bits are NANDed with 0 which means that they are 1 in the result. Which accordingly is 11111.....1. For a signed integer this is the binary representation of -1. An odd number hat 1 as its last bit. NANDing it with 1 means (look at the table again) that the last bit of the result is 0. All other bits are NANDed with 0 which means that they are 1 in the result. Which accordingly is 1111...0. For a signed integer this is the binary representation of -2. In other words: x NAND 1 yields -1 if x is even, and -2 if x is odd. This property is used in the program. You can also do this on the 34s. In normal real mode (DECM, H.d) NAND acts as a logical NAND, i.e. 0 is treated as 0 and any other number is the same as 1. The result is either 0 or 1, just as the table says. However, you can use the 35s method after switching to an integer mode: In Base-10 mode you will get the same results as the 35s. But on a 34s there are other ways to achieve the same result, e.g. 2 MOD INC X +/–. All in all, this NAND method is just a small trick to show off a rarely used command in order to save one step or two. ;-) Dieter |
|||
06-16-2016, 06:51 AM
(This post was last modified: 06-16-2016 06:53 AM by ggauny@live.fr.)
Post: #8
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Hi,
Well, well. Thank you very much for reply Dieter. Indeed, a very smart trick ! Gérard. |
|||
06-16-2016, 06:59 AM
Post: #9
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Thank you Dieter for the clear explanation. Cheers.
|
|||
06-16-2016, 10:34 AM
Post: #10
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Hi,
But this Boolean operation is not implemented in the HP Prime. So I'm looking to make a define user. BTW I've failed in "Abitur" because maths and English, the rest was almost good. I will try next year. Good day. Gérard. |
|||
06-17-2016, 11:46 AM
Post: #11
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
Alas, 02.022016 31.082016 give 6m and 29 d
and 02.022015 31.082015 give the same result : 6m and 29d I think that in 2016 is 28 d and in 2015 is 27 d (and six months). Gérard. |
|||
06-17-2016, 06:09 PM
(This post was last modified: 06-17-2016 06:19 PM by Dieter.)
Post: #12
|
|||
|
|||
RE: HP 35s: years, months and days between two dates
(06-17-2016 11:46 AM)ggauny@live.fr Wrote: Alas, 02.022016 31.082016 give 6m and 29 d I would say that 6 months and 29 days is the correct answer in both cases. It's 6 months from 2 Feb to 2 Aug, and then 29 days until 31 Aug. Both in a leap year and in a common year. Things are different if day2 is less than day1. For instance from 20.022015 to 10.082015 it's 5 months and 18 days, while from 20.022016 to 10.082016 it's 5 months and 19 days. Dieter |
|||
08-16-2017, 01:51 PM
Post: #13
|
|||
|
|||
RE: (35S) years, months and days between two dates
Hello! i dont how to write the 1E4 in line Y035. How i do this in my Hp35s?
I think i'm doing wrong there becase my results arent good. |
|||
08-16-2017, 06:28 PM
Post: #14
|
|||
|
|||
RE: (35S) years, months and days between two dates | |||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 6 Guest(s)