Post Reply 
Year(s) Month(s) Day(s) between 2 dates
10-21-2015, 07:22 PM (This post was last modified: 10-22-2015 12:30 PM by Dieter.)
Post: #9
RE: Year(s) Month(s) Day(s) between 2 dates
(10-19-2015 09:33 AM)ggauny@live.fr Wrote:  Hi, I've ported routine of mine from HP41cx to WP34s.

Your program obviously uses a 0 or 1 in register D as a kind of carry bit. This can be done in a simple and more effective way by using a flag. Here is my version for the 34s. It computes the number of days in a month according to the previously posted formula. Flag 00 serves as a kind of carry flag.

Code:
01 LBL"YMD"
02 STO 02
03 DAY
04 x<> Y
05 STO 01
06 DAY
07  -
08 CF 00
09 x≥0?       ; d2 ≥ d1?
10 GTO 01     ; no adjustment required
11 SF 00      ; else set carry flag
12 RCL 02
13 MONTH
14 DEC X      ; month before m2
15 x≠0?
16 SKIP 002
17 DROP       ; if zero
18 #012       ; it's december
19 #002
20 x≠? Y      ; if it's not February
21 GTO 00     ; compute no. of days in month
22 c DROP     ; else drop two levels (complex drop)
23 #028       ; February has 28 days
24 RCL 02
25 LEAP?      ; in leap years
26 INC Y      ; add one day
27 DROP
28  +         ; add #days to d2-d1
29 GTO 01     ; and continue
30 LBL 00
31 DROP
32 RCL X     ;  compute no. of days in month
33 #008
34 IDIV
35  +
36 #002
37 MOD
38 #030
39  +         ; = no. of days in month before m2
40  +         ; add this to d2-d1
41 LBL 01
42 SDR 004    ; move days to 0,00dd
43 RCL 02
44 MONTH
45 FS?C 00
46 DEC X      ; adjust m2 if carry flag set
47 RCL 01
48 MONTH
49  -
50 x≥0?
51 SKIP 003
52 SF 00      ; set carry flag if m2-m1 negative
53 #012       ; add back 12 months
54  +
55 SDR 002    ; move months to 0,mm
56  +         ; add days to get 0,mmdd
57 RCL 02
58 YEAR
59 FS?C 00
60 DEC X      ; adjust y2 if carry flag set
61 RCL 01
62 YEAR
63  -
64  +         ; add years to get y,mmdd
65 END

date1 [ENTER] date2 XEQ"YMD" => y,mmdd
 

Using the WP34s ΔDAYS function, the code gets a bit shorter:

Code:
01 LBL"YMD"
02 STO 02
03 DAY
04 x<> Y
05 STO 01
06 DAY
07  -
08 CF 00
09 x≥0?       ; d2 ≥ d1?
10 GTO 01     ; no adjustment required
11 SF 00      ; else set carry flag
12 RCL 02
13 DATE->
14 DROP
15 DSZ X      ; month before m2
16 SKIP 003   ; is it > 0, i.e. Jan...Nov? Then continue
17 DROP
18 #012       ; if not, month is December
19 DEC Y      ; of previous year
20 RCL 01
21 DAY        ; now stack holds d1, m2-1, year of m2-1
22 ->DATE     ; generate new date = d1 in month before m2
23 RCL 02
24 ΔDAYS      ; days between date2 and d1 in month before date2
25 LBL 01
26 SDR 004    ; move days to 0,00dd
27 RCL 02
28 MONTH
29 FS?C 00
30 DEC X      ; adjust m2 if carry flag set
31 RCL 01
32 MONTH
33  -
34 x≥0?
35 SKIP 003
36 SF 00      ; set carry flag if m2-m1 negative
37 #012       ; add back 12 months
38  +
39 SDR 002    ; move months to 0,mm
40  +         ; add days to get 0,mmdd
41 RCL 02
42 YEAR
43 FS?C 00
44 DEC X      ; adjust y2 if carry flag set
45 RCL 01
46 YEAR
47  -
48  +         ; add years to get y,mmdd
49 END

Edit: corrected an error in the final steps of both programs.

In both cases your Louis XIV example returns 72,0318, i.e. 72 years, 3 months and 18 days.
As usual, comments and corrections are welcome.

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


Messages In This Thread
RE: Year(s) Month(s) Day(s) between 2 dates - Dieter - 10-21-2015 07:22 PM



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