(HP-65) Date Functions - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP-65/67/97 Software Library (/forum-12.html) +--- Thread: (HP-65) Date Functions (/thread-14304.html) |
(HP-65) Date Functions - Dave Britten - 01-03-2020 04:11 PM These programs are mostly adapted from the HP-67 Standard Pac calendar functions card SD-04A (or at least the algorithms are; the program code is quite different). You will need to use two cards, or write to both sides of a single card, which seems to work fine. Recommended key labels: Card 1 A: ΔDAYS B: DT→D# C: DT→DOW D: D#→DOW Card 2 A: D#→DT B: ΔD→ΔW.D Calculations are accurate for dates between Mar. 1, 1900 through Feb. 28, 2100. Some limited calculations outside this range can be done accurately, but the program incorrectly treats all years divisible by 100 as leap years, whereas years divisible by 100 but not divisible by 400 are not leap years. Day-of-week calculations will be incorrect outside the supported date range. Usage All dates are entered in MM.DDYYYY format. You may ignore any instructions to load a card if that card is currently already loaded. Calculate the number of days between two dates Load card 1. Enter the earlier date, press ENTER, enter the later date, and press A. The number of days will be returned in X. Example: How many days remaining until the 2020 US elections? 1.032020 ENTER 11.032020 A: 305 Calculate a day number for a given date Load card 1. Enter the date into X, and press B. The day number will be returned in X. The value in the Y register will be preserved. Example: What is the day number of the 2020 summer solstice in the northern hemisphere? 6.202020 B: 738039 These day numbers can be used with card 2 to calculate future/past dates relative to a starting date. If you wish to convert the day numbers used by this program to Julian day numbers, simply add 1720982. Calculate the day of the week for a given date Load card 1. Enter the date into X, and press C. The day of the week will be returned in X. Sunday is 0, Monday is 1, proceeding through Saturday, which is 6. Example: What day of the week is the 2020 vernal equinox in the northern hemisphere? 3.192020 C: 4 (Thursday) Calculate the day of the week for a given day number Load card 1. With the day number in X (e.g. after computing a day number by pressing B), press D to calculate the day of the week. Output is the same as converting a date to the day of the week. Calculate the date a given number of days before/after a starting date Load card 1. Enter the starting date into X, and press B to compute the day number. Optional: save this day number in R7 or R8 if you intend to do multiple calculations with the same starting date. Load card 2. Perform addition/subtraction on the day number in X. Optional: save the new day number in R7 or R8 if you wish to calculate multiple intervals. Press A and the new day number in X will be converted to a date in MM.DDYYYY format and returned in X. If you saved a day number in R7 or R8, you may recall the number and perform additional calculations/conversions. Example: What will be the date 57 days from now? 90 days from now? Load card 1. 1.032020 B: 737870 STO 7 Load card 2. 57 +: 737927 A: 2.292020 Feb. 29, 2020 (Note: you may need to press DSP . 6 to see the full date.) RCL 7: 737870 90 +: 737960 A: 4.022020 Apr. 2, 2020 Calculate the number of weeks + days between two dates Load card 1. Enter the earlier date, press ENTER, and enter the later date. Press A to calculate the number of days between the two dates. Load card 2. Press B to convert the number of days to number of weeks + days in W.D format. Example: How many weeks + days until June 1st? Load card 1. 1.032020 ENTER 6.012020 A: 150 Load card 2. B: 21.3 (21 weeks and 3 days) Program Code Code: CARD #1 RE: (HP-65) Date Functions - Don Shepherd - 01-03-2020 04:23 PM I tip my hat to you, Dave, you are "da" man. I started to enter an equation yesterday from a reference I pointed out to you and I gave up part way through because I knew I couldn't possibly fit all the required code in 100 (or 200) steps. I'll run some tests on these two programs today and verify. Don RE: (HP-65) Date Functions - Dave Britten - 01-03-2020 04:46 PM (01-03-2020 04:23 PM)Don Shepherd Wrote: I tip my hat to you, Dave, you are "da" man. I started to enter an equation yesterday from a reference I pointed out to you and I gave up part way through because I knew I couldn't possibly fit all the required code in 100 (or 200) steps. Thanks, let me know if you find any weird edge cases that break it. I had to ditch the 1720982 day offset for true Julian day numbers in order to free up enough steps to fit everything in. Consequently, the day-of-week calculation changes from (D+1) mod 7 to (D+5) mod 7. Seems to work, though. I can't decide if I want to make the day-number-to-date routine save the previous X value in Y. I might have to trim a couple of steps to make that fit. RE: (HP-65) Date Functions - Albert Chan - 01-04-2020 01:03 PM (01-03-2020 04:11 PM)Dave Britten Wrote: Calculations are accurate for dates between Mar. 1, 1900 through Feb. 28, 2100. Some limited calculations outside this range can be done accurately, but the program incorrectly treats all years divisible by 100 as leap years, whereas years divisible by 100 but not divisible by 400 are not leap years. Day-of-week calculations will be incorrect outside the supported date range. A simple trick to manually make it work for all dates: Gregorian calendar have a period of 400 years, (97 leap years → 146097 days/(4 century)) For 22nd, 23rd, 24th century, we have 24 leap years / century → 36524 days/century Example: day number of Jan 2, 3456 3456 = 2056 + 3*400 + 200 → day(Jan 2, 3456) = day(Jan 2, 2056) + 3*146097 + 2*36524 Example: day number of July 4, 1776 1776 = 1976 - 200 → day(July 4, 1776) = day(July 4, 1976) - 2*36524 Example: day number of Jan 1, 1900 We could "move up" 1 century to 2000, but then we need to check the months and the days too. A simpler way is shoot high, and adjust between Feb 29, 2000 to Feb 28, 2100. 1900 = 2100 - 400 + 200 → day(Jan 1, 1900) = day(Jan 1, 2100) - 146097 + 2*36524 Update: shoot low and adjust between Mar 1, 1900 to Feb 28, 2000 works too. → day(July 4, 1776) = day(July 4, 1976) - 2*36524 → day(Jan 1, 1900) = day(Jan 1, 2000) - 36524 Combine both approaches, using full valid date, Mar. 1, 1900 through Feb. 28, 2100 day(date) = day(closest centuries shifted valid date) - IP(36524.25 * shifted centuries) → day(Jan 2, 3456) = day(Jan 2, 2056) - IP(36524.25 * (20-34)) = day(Jan 2, 2056) + 511339 |