Two things called "Julian date", and a holiday that falls in the wrong year
I run TimeCalc, a small independent site with free date and time calculators. Two of them were more interesting to build than I expected, so here's a short write-up. 1. "Julian date" means two different things
I run TimeCalc, a small independent site with free date and time calculators. Two of them were more interesting to build than I expected, so here's a short write-up.
1. "Julian date" means two different things
-
On food packaging, in military logistics and on mainframes it's an ordinal date: year + day of the year, written
YYDDD.26307is day 307 of 2026, i.e. November 3, 2026. -
In astronomy it's the Julian Day Number: a running count of days since noon on January 1, 4713 BC. January 1, 2000 is
2451545.
People searching for one often get the other, so the page shows both: YYDDD, YYYYDDD and the Julian Day Number. It converts in both directions: type 26307 or pick a date.
If you already count days since the Unix epoch, the Julian Day Number takes one addition:
type Ymd = { y: number; m: number; d: number };
// Days since 1970-01-01 (UTC) for a calendar date
const dayNumber = ({ y, m, d }: Ymd) => {
const t = new Date(0);
t.setUTCFullYear(y, m - 1, d); // not Date.UTC: Date.UTC(99, 0, 1) is 1999
return Math.round(t.getTime() / 86_400_000);
};
const julianDayNumber = (date: Ymd) => dayNumber(date) + 2_440_588;
julianDayNumber({ y: 2000, m: 1, d: 1 }); // 2451545
The catch with YYDDD is leap years. Every day after February 28 moves by one, so 061 is March 2 in 2026 but March 1 in 2028.
2. Business days, and a holiday in the previous year
It counts business days between two dates, or adds N business days to a date. It skips weekends and the 11 US federal holidays and lists which holidays it skipped.
Two decisions I had to make:
Which end counts? By default the start date is excluded and the end date included, because that's how "ships in 5 business days" is usually read. Excel's NETWORKDAYS counts both ends, so there's a checkbox to match it.
Observed dates. A holiday on a Saturday is observed on the Friday before, and one on a Sunday on the Monday after. Independence Day 2026 is a Saturday, so it's observed on Friday, July 3.
Observed dates lead to the edge case I like most: New Year's Day 2028 is a Saturday, so it's observed on Friday, December 31, 2027. It's a 2028 holiday that falls in 2027. If you only generate holidays for the years in your range, you miss it. So I generate one extra year on each side:
const holidaySet = (fromYear: number, toYear: number) => {
const set = new Map<number, string>();
for (let y = fromYear - 1; y <= toYear + 1; y++)
usFederalHolidays(y).forEach((h) => set.set(h.day, h.name));
return set;
};
That's why 2027 has 249 business days and 2026 has 250.
Stack
Static Astro pages, with each tool written as a custom element in plain TypeScript and no UI framework. Everything is calculated in the browser. There are no accounts and no ads. The date math and holiday rules have unit tests (node --test).
Feedback welcome
- Which edge cases did I miss?
- Would holidays for other countries be useful to you?
- Have you seen other "Julian date" formats in the wild?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.