Method Deck / dates

Days between two dates

Counts the whole days between two calendar dates, inclusive or exclusive of both endpoints.

Published for review. The Founder is checking every card himself; anything he strikes comes down the same hour.

Try it

Inputs

Outputs

Steps

  1. Parse date_a and date_b as proleptic Gregorian civil dates (year, month, day).
  2. Convert each date to a Julian Day Number (JDN) using the Fliegel and Van Flandern (1968) day-count method, implemented here via the equivalent Hinnant days_from_civil algorithm (cited above).
  3. Require date_b to be on or after date_a; otherwise the inputs are invalid.
  4. diff = JDN(date_b) - JDN(date_a). This is the exclusive count: the number of nights, or day-boundaries crossed, between the two dates.
  5. If inclusive is true, days = diff + 1 (both endpoint calendar days are counted). If inclusive is false, days = diff.

Constants

None.

Sources

Worked example and test vectors

NameInputsAnswer
worked example{"date_a":"2024-01-01","date_b":"2024-01-10","inclusive":false}9
test vector: inclusive count{"date_a":"2024-01-01","date_b":"2024-01-10","inclusive":true}10
test vector: leap day crossing{"date_a":"2024-02-28","date_b":"2024-03-01","inclusive":false}2

Published card