Post Reply 
(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
Y002  STO B
Y003  IP
Y004  STO- B
Y005  x<>y
Y006  STO A
Y007  IP
Y008  STO- A
Y009  -         day2 - day1
Y010  STO D     #days, may be negative
Y011  100
Y012  STOx A
Y013  STOx B
Y014  RCL B
Y015  IP
Y016  STO- B
Y017  RCL A
Y018  IP
Y019  STO- A
Y020  STO C     save month1
Y021  -         month2 - month1
Y022  RCL D
Y023  SGN
Y024  x>0?
Y025  CLx       carry = -1 if day2 < day1, or 0 otherwise
Y026  +         add -1 or 0
Y027  STO M
Y028  SGN
Y029  12
Y030  x         carry = -12 if month2 < month1, or 0 otherwise
Y031  x>0?
Y032  CLx
Y033  STO- M    adjust #months by 12 or 0
Y034  SGN       new carry = -1 or 0
Y035  1E4
Y036  STOx A
Y037  STOx B
Y038  RCL B
Y039  IP
Y040  STO B
Y041  RCL A
Y042  IP
Y043  STO A
Y044  -         year2 - year1
Y045  R↑        recall carry
Y046  +         and add
Y047  STO Y     #years
Y048  RCL D
Y049  x≥0?      day2 ≥ day1?
Y050  GTO Y080  then jump to exit
Y051  2         else adjust #days by no. of days in month1
Y052  RCL C
Y053  x=y?      month1 = Feb?
Y054  GTO Y061
Y055  8
Y056  INT÷      compute no. of days in month1
Y057  RCL+ C    days in month = 30 + (month + month div 8) mod 2
Y058  x<>y
Y059  RMDR
Y060  GTO Y077  finally add 30 and quit
Y061  RCL A     handle February separately
Y062  400
Y063  RMDR
Y064  SGN       check if
Y065  RCL A     year1
Y066  100       is a
Y067  RMDR      leap year
Y068  SGN
Y069  +         days in February
Y070  RCL A     = 29 - [sgn(y mod 400) + sgn(y mod 100) + sgn(y mod 4)] mod 2
Y071  4         = 30 + [sgn(y mod 400) + sgn(y mod 100) + sgn(y mod 4)] NAND 1
Y072  RMDR
Y073  SGN
Y074  +
Y075  1
Y076  NAND      = -1 in leap years, or -2 otherwise
Y077  30
Y078  +         = no. of days in month1
Y079  STO+ D    adjust #days
Y080  RCL D     Exit routine starts here
Y081  1E4
Y082  ÷
Y083  RCL M     build y,mmdd
Y084  100
Y085  ÷
Y086  +
Y087  RCL+ Y
Y088  RCL D     Fill stack
Y089  RCL M     with results
Y090  RCL Y     Y, M and D
Y091  R↑        and y,mmdd
Y092  RTN

Variables:

A: date 1
B: date 2
C: month 1

D: => #days
M: => #months
Y: => #years

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
[XEQ] Y [ENTER]

      T: #days
      Z: #months
=>    Y: #years
      X: y.mmdd

Example:
How many years, months and days have elapsed between 25 February 1999 and 13 November 2005?

Code:
25.021999 [ENTER] 13.112005
[XEQ] Y [ENTER]

=> 6,0816

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).
Find all posts by this user
Quote this message in a reply
06-07-2016, 01:13 PM
Post: #2
RE: HP 35s: years, months and days between two dates
Thank you.
Visit this user's website Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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
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).

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
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
08-16-2017, 06:28 PM
Post: #14
RE: (35S) years, months and days between two dates
(08-16-2017 01:51 PM)Bonnie Wrote:  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.
Just press 1 E 4 before line Y035

Pedro
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 3 Guest(s)