Post Reply 
Number of Days After January 1
03-14-2019, 07:20 PM
Post: #4
RE: Number of Days After January 1
(03-11-2019 02:25 AM)Eddie W. Shore Wrote:  If M = 1 and M = 2 Then
DATE# = int(30.6 * M + 368.8) + D - 400

Otherwise,
DATE# = int(30.6 * M + 1.6) +D - 35 (non-leap year)
DATE# = int(30.6 * M + 1.6) + D - 34 (leap year)

Eddie, you can simplify these formulas significantly.
Since D and 400 are positive integers...

int(30.6 * M + 368.8) + D - 400
= int(30.6 * M + 368.8 + D - 400)
= int(30.6 * M + D - 31.2)

Or here (D=1 or 2) simply

= int(30.6 * M + D - 31)

The two other formulas can be simplified the same way.
Or they can be derived from the Jan/Feb formula.

With L=0 (common year) or 1 (leap year):

days = 30.6 * M + D - 31
if M >= 3 then days = days - 2.3 + L
return int(days)

Note: according to your formulas the final 2.3 would be 2.4, but subtracting only 2.3 leaves a fractional part between 0.1 and 0.9 so that roundoff problems are avoided (e.g. in non-decimal environments) that may lead to cases like int(242.9999999....) for 1 September.

Here is a version of your HP-41C program that implements this idea.

Code:
01 LBL "DAYD"
02 "MONTH↑DAY ?"
03 PROMPT
04 RCL Y
05 30.6
06 *
07 +
08 31
09 -
10 X<>Y
11 3
12 X>y?
13 GTO 01
14 2.3
15 ST- T
16 R↑
17 "LEAP YR 0/1?"
18 PROMPT
19 +
20 STO Z
21 LBL 01
22 RCL Z
23 INT
24 END

XEQ "DAYD"
MONTH↑DAY ?
10 [ENTER] 1 [R/S]
LEAP YR 0/1?
0 [R/S]

=> 273


Note that the program only prompts for a leap year input if this is required, i.e. for months between March and December.

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


Messages In This Thread
RE: Number of Days After January 1 - Dieter - 03-14-2019 07:20 PM



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