Post Reply 
Year(s) Month(s) Day(s) between 2 dates
10-28-2015, 12:11 PM
Post: #33
RE: Year(s) Month(s) Day(s) between 2 dates
(10-27-2015 11:25 PM)Dieter Wrote:  The second case indeed throws a Domain Error since the program tried to calculate the difference between 31 February (!) and 1 March 2015. This should get fixed. Also changing the code to the alternative method you prefer is quite simple.

Here is an updated version with a fix for the Domain Error at the expense of 3 additional steps for a total of 40 steps:

Code:
01 LBL"YMD"   ; stack: date2, date1
02 LocR 004   ; allocate local registers R.00 to R.03
03 cSTO.00    ; store date2 in R.00 and date1 in R.01
04 DATE->     ; stack: d2, m2, y2, date1
05 <>YZTX     ; stack: m2, y2, date1, d2
06 cSTO.02    ; store m2 in R.02 and y2 in R.03
07 cX<>Z      ; stack: date1, d2, m2, y2
08 DAY        ; stack: d1, d2, m2, y2
09  -         ; stack: d2-d1, m2, y2 – LastX: d1
10 x≥0?       ; d2 ≥ d1?
11 SKIP 014   ; no adjustment required, goto step 26
12 DEC.02     ; adjust m2 in R.02
13 DROP       ; stack: m2, y2 – LastX: d1
14 Z<> L      ; stack: m2, y2, d1 
15 #01        ; stack: 1, m2, y2, d1 
16 ->DATE     ; 1st day of month m2 in year y2
17 #01
18 +/-
19 DAYS+      ; 
20 DATE->     ; stack: last day of month m2-1, m2-1, y2(-1), d1
21 RCL T      ; 
22 MIN        ; min(d1, last day of m2-1)
23 ->DATE     ; generate new date = d1 in month before m2
24 RCL.00
25 ΔDAYS      ; days between date2 and d1 in month before date2
26 SDR 004    ; move days to 0,00dd
27 RCL.01     ; recall date1
28 DATE->     ; stack: d1, m1, y1, 0,00dd
29 DROP       ; stack: m1, y1, 0,00dd
30 cSTO-.02   ; R.02: m2-m1, R.03: y2-y1
31 cX<>.02    ; stack: m2-m1, y2-y1, 0,00dd
32 x≥0?       ; stack: m2-m1, y2-y1, 0,00dd
33 SKIP 003
34 DEC Y      ; adjust y2-y1 if m2-m1 negative
35 #012       ; add back 12 months
36  +         ; stack: mm, y2-y1, 0,00dd 
37 SDR 002    ; move months to 0,mm
38  +         ; add y2-y1  to get y,mm 
39  +         ; add days to get y,mmdd
40 END        ; return y,mmdd

Compared to Franz version in 29 steps, this one is independent of the input date format as long as it is consistent with the WP 34S current Mode (output format is always YMD), and the results are aligned with WolframAlpha:

in D.MY format:
18.071978 to 19.102015 --> 37.0301
14.051643 to 01.091715 --> 72.0318
23.021951 to 05.091999 --> 48.0613
01.012000 to 01.022000 --> 0.01
31.012015 to 01.032015 --> 0.0101
31.012015 to 01.042015 --> 0.0201

in M.DY format:
07.181978 to 10.192015 --> 37.0301
01.312015 to 03.012015 --> 0.0101
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 - Didier Lachieze - 10-28-2015 12:11 PM



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