Number of Days After January 1 - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP-41C Software Library (/forum-11.html) +--- Thread: Number of Days After January 1 (/thread-12603.html) |
Number of Days After January 1 - Eddie W. Shore - 03-11-2019 02:25 AM Introduction The program DATENO calculates the number of days from January 1. The program prompts whether we are working in a leap year or not. With D = Day and M = Month, the days between January 1 and any other date within the calendar year is: 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) HP 41C/DM 41L Program: DATENO (^T: beginning of an alpha string) Code: 01 LBL^T DATENO Examples Days between January 1 and February 16 (M = 2, D = 16): 46 Days between January 1 and October 1 (M = 10, D = 1): (Non-leap year): 273 (Leap year): 274 Link to blog post: https://edspi31415.blogspot.com/2019/03/ti-84-plus-and-hp-41c-number-of-days.html RE: Number of Days After January 1 - Dieter - 03-11-2019 06:53 PM (03-11-2019 02:25 AM)Eddie W. Shore Wrote: The program DATENO calculates the number of days from January 1. Maybe you should add 1 to the result to get the more common "day of year", i.e. 1 January = day 1 and 31 December = day 365. (03-11-2019 02:25 AM)Eddie W. Shore Wrote: If M = 1 and M = 2 Then What about 31*M + D -32 ?-) Anyway, here is my attempt at a day-of-year program. I have derived the formulas myself, so beware. ;-) Please try it and report any errors you find. Code: 01 LBL "DOY" Usage: day [ENTER] month [ENTER] year XEQ "DOY" If you prefer month, day, year instead simply insert an X<>Y command between line 08 and 09. The program automatically checks for leap years, this works for all dates between 1 January 1901 and 28 February 2100. Example: 1 [ENTER] 10 [ENTER] 1999 XEQ"DOY" => 274 1 [ENTER] 10 [ENTER] 2000 XEQ"DOY" => 275 Dieter Number of Days After March 1st - Thomas Klemm - 03-11-2019 08:30 PM (03-11-2019 02:25 AM)Eddie W. Shore Wrote: The program DATENO calculates the number of days from January 1. Somewhat related is the following program from the article Sunrise and Sunset. Here it is adapted for the HP-42S: Code: 00 { 30-Byte Prgm } Examples: 1.03 R/S 1 28.02 R/S 365 29.02 R/S 366 This avoids having to deal with leap years. Cheers Thomas RE: Number of Days After January 1 - Dieter - 03-14-2019 07:20 PM (03-11-2019 02:25 AM)Eddie W. Shore Wrote: If M = 1 and M = 2 Then 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" 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 |