(HP-65) Date Functions
01-03-2020, 04:11 PM
Post: #1
 Dave Britten Senior Member Posts: 1,865 Joined: Dec 2013
(HP-65) Date Functions
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?

1.032020 B: 737870
STO 7
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?

1.032020 ENTER
6.012020 A: 150
B: 21.3 (21 weeks and 3 days)

Program Code

Code:
CARD #1 LBL     23 A       11 B       12 g x><y  35 07 B       12 -       51 RTN     24 LBL     23 B       12 g X><Y  35 07 STO 1   33 01 g X><Y  35 07 ENTER   41 f       31 INT     83 STO 2   33 02 -       51 EEX     43 2       02 *       71 ENTER   41 f       31 INT     83 STO 3   33 03 -       51 EEX     43 4       04 *       71 STO 4   33 04 2       02 RCL 2   34 02 g x>y   35 24 GTO     22 1       01 1       01 3       03 STO     33 +       61 2       02 1       01 STO     33 -       51 4       04 GTO     22 6       06 LBL     23 1       01 1       01 STO     33 +       61 2       02 LBL     23 6       06 RCL 4   34 04 3       03 6       06 5       05 .       83 2       02 5       05 *       71 f       31 INT     83 RCL 2   34 02 3       03 0       00 .       83 6       06 0       00 0       00 1       01 *       71 f       31 INT     83 +       61 RCL 3   34 03 +       61 RCL 1   34 01 g X><Y  35 07 RTN     24 LBL     23 C       13 B       12 LBL     23 D       14 5       05 +       61 ENTER   41 ENTER   41 7       07 /       81 f       31 INT     83 7       07 *       71 -       51 RTN     24 CARD #2 LBL     23 A       11 STO 1   33 01 1       01 2       02 2       02 .       83 1       01 -       51 3       03 6       06 5       05 .       83 2       02 5       05 STO 5   33 05 /       81 f       31 INT     83 STO 4   33 04 RCL 1   34 01 RCL 4   34 04 RCL 5   34 05 *       71 f       31 INT     83 STO 5   33 05 -       51 3       03 0       00 .       83 6       06 0       00 0       00 1       01 STO 6   33 06 /       81 f       31 INT     83 STO 2   33 02 RCL 1   34 01 RCL 5   34 05 -       51 RCL 6   34 06 RCL 2   34 02 *       71 f       31 INT     83 -       51 STO 3   33 03 RCL 2   34 02 1       01 -       51 1       01 2       02 g x><y  35 07 g x>y   35 24 g x><y  35 07 -       51 STO 2   33 02 2       02 g x><y  35 07 g x>y   35 24 GTO     22 1       01 1       01 STO     33 +       61 4       04 LBL     23 1       01 RCL 4   34 04 EEX     43 4       04 /       81 RCL 3   34 03 +       61 EEX     43 2       02 /       81 RCL 2   34 02 +       61 RTN     24 LBL     23 B       12 7       07 /       81 ENTER   41 f-1     32 INT     83 -       51 g LSTX  35 00 .       83 7       07 *       71 +       61 RTN     24
01-03-2020, 04:23 PM
Post: #2
 Don Shepherd Senior Member Posts: 741 Joined: Dec 2013
RE: (HP-65) Date Functions
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
01-03-2020, 04:46 PM
Post: #3
 Dave Britten Senior Member Posts: 1,865 Joined: Dec 2013
RE: (HP-65) Date Functions
(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.

I'll run some tests on these two programs today and verify.

Don

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.
01-04-2020, 01:03 PM (This post was last modified: 01-14-2020 04:41 PM by Albert Chan.)
Post: #4
 Albert Chan Senior Member Posts: 1,489 Joined: Jul 2018
RE: (HP-65) Date Functions
(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
 « Next Oldest | Next Newest »

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