Code · JavaScript
Julian date in JavaScript
JavaScript for both kinds of “Julian date”: the day-of-year code (YYYYDDD, YYDDD, JDE CYYDDD) in UTC, local time or any time zone, and the astronomical Julian Date (JD) and Modified Julian Date (MJD). Works in browsers and Node.js with no libraries.
- Day-of-year (ordinal) date: the year plus the day
number 1–365/366, written
2026202(YYYYDDD) or26202(YYDDD). July 21, 2026 is day 202. - Julian Date: a running count of days since noon UTC
on January 1, 4713 BC. July 21, 2026 at 00:00 UTC is
2461242.5. See Julian Day for the background.
Decide on UTC or local time first
getFullYear(), getMonth() and
getDate() read the device’s time zone; the
getUTC* methods read UTC. The same instant can fall on
different days in each. Use UTC for stored data and server code, and
local time when showing a person “today”. Read the year,
month and day all from the same one.
Day of year
Subtract midnight UTC of “day 0” (December 31 of the previous year) and divide by the length of a day. Because both values are UTC midnights, the result is always a whole number:
function dayOfYear(d) {
const start = Date.UTC(d.getUTCFullYear(), 0, 0);
const diff = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()) - start;
return diff / 86400000;
}
dayOfYear(new Date('2026-07-21')); // 202 YYYYDDD and YYDDD
const pad3 = (n) => String(n).padStart(3, '0');
function toYYYYDDD(d) {
return `${d.getUTCFullYear()}${pad3(dayOfYear(d))}`;
}
function toYYDDD(d) {
const yy = String(d.getUTCFullYear() % 100).padStart(2, '0');
return `${yy}${pad3(dayOfYear(d))}`;
}
toYYYYDDD(new Date('2026-07-21')); // "2026202"
toYYDDD(new Date('2005-01-05')); // "05005" Today in local time or a specific time zone
For a page that tells a visitor today’s Julian date, use their
local date. Feeding the local year, month and day into
Date.UTC keeps daylight-saving changes out of the count:
// Today in the visitor's own time zone (what a person expects to see)
function localJulian(d = new Date()) {
const doy = (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())
- Date.UTC(d.getFullYear(), 0, 0)) / 86400000;
return `${d.getFullYear()}${String(doy).padStart(3, '0')}`;
}
localJulian(); // e.g. "2026202"
For a fixed location, such as a plant or a warehouse, read the date in
that zone with Intl.DateTimeFormat:
// Today in a named time zone, e.g. for a plant in Chicago
function julianInZone(timeZone, d = new Date()) {
const parts = new Intl.DateTimeFormat('en-US', {
timeZone, year: 'numeric', month: 'numeric', day: 'numeric',
}).formatToParts(d);
const get = (type) => Number(parts.find((p) => p.type === type).value);
const y = get('year'), m = get('month'), day = get('day');
const doy = (Date.UTC(y, m - 1, day) - Date.UTC(y, 0, 0)) / 86400000;
return `${y}${String(doy).padStart(3, '0')}`;
}
const t = new Date('2026-07-22T02:30:00Z');
julianInZone('America/Chicago', t); // "2026202" (still July 21 there)
julianInZone('Asia/Tokyo', t); // "2026203" Parse a code back to a Date
setUTCFullYear(year, 0, day) rolls the day of year across
the months for you. It is used instead of Date.UTC because
Date.UTC treats years 0–99 as 1900–1999:
Date.UTC(26, 0, 202) is in 1926.
function fromYYYYDDD(code) {
const n = Number(code);
const d = new Date(Date.UTC(2000, 0, 1));
d.setUTCFullYear(Math.floor(n / 1000), 0, n % 1000); // day 202 of January = July 21
return d;
}
function fromYYDDD(code, pivot = 68) {
const n = Number(code); // "05001" and 5001 both work
const yy = Math.floor(n / 1000);
return fromYYYYDDD((yy <= pivot ? 2000 + yy : 1900 + yy) * 1000 + (n % 1000));
}
fromYYYYDDD('2026202').toISOString(); // "2026-07-21T00:00:00.000Z"
fromYYDDD(5001).toISOString(); // "2005-01-01T00:00:00.000Z" JD Edwards (CYYDDD)
// Oracle JD Edwards CYYDDD: C = centuries since 1900
const toJde = (d) => (d.getUTCFullYear() - 1900) * 1000 + dayOfYear(d);
const fromJde = (jde) => (Number(jde) === 0 ? null : fromYYYYDDD(Number(jde) + 1900000));
toJde(new Date('2026-07-21')); // 126202
fromJde('99365').toISOString(); // "1999-12-31T00:00:00.000Z" More on the format on the JDE Julian date converter page.
With Temporal
Temporal is the modern date API for JavaScript, with a
built-in dayOfYear. It has shipped in recent Firefox and
Chrome releases; check current support for the browsers and Node.js
version you target, and use the @js-temporal/polyfill
package where it is missing.
// Temporal: dates without times or time-zone surprises
const d = Temporal.PlainDate.from('2026-07-21');
d.dayOfYear; // 202
d.daysInYear; // 365
`${d.year}${String(d.dayOfYear).padStart(3, '0')}`; // "2026202"
// Today in a given time zone
Temporal.Now.plainDateISO('America/New_York').dayOfYear;
// YYYYDDD back to a date
Temporal.PlainDate.from({ year: 2026, month: 1, day: 1 }).add({ days: 201 }).toString(); // "2026-07-21" Astronomical Julian Date (JD) and MJD
A Date holds milliseconds since 1970-01-01 00:00 UTC, the
Unix epoch, whose Julian Date is exactly 2440587.5. So the
conversion is one line:
const toJulianDate = (date) => date.getTime() / 86400000 + 2440587.5;
const toMJD = (date) => toJulianDate(date) - 2400000.5;
const julianDayNumber = (date) => Math.floor(toJulianDate(date) + 0.5); // JDN of the UTC date
const d = new Date('2026-07-21T00:00:00Z');
toJulianDate(d); // 2461242.5
toMJD(d); // 61242
julianDayNumber(d); // 2461243
toJulianDate(new Date()); // the JD right now
The Julian Day Number is the integer for the calendar date: it is the
JD at noon UTC. Math.floor(jd + 0.5) gives the JDN of the
current UTC date; plain Math.floor(jd) gives the number of
the Julian day in progress, which changes at noon. The MJD subtracts
2400000.5 and changes at midnight (see
MJD).
JD or MJD back to a Date
const fromJulianDate = (jd) => new Date(Math.round((jd - 2440587.5) * 86400000));
const fromMJD = (mjd) => new Date(Math.round((mjd - 40587) * 86400000));
fromJulianDate(2461242.5).toISOString(); // "2026-07-21T00:00:00.000Z"
fromMJD(61242.75).toISOString(); // "2026-07-21T18:00:00.000Z" Rounding to the millisecond removes floating-point noise. A JD stored as a double is precise to about 40 microseconds, so this is lossless for practical purposes.
Dates outside the Date range
Date covers about 273,000 years either side of 1970. For
arbitrary year, month and day numbers, use the Meeus formula, which
needs only arithmetic:
// Meeus: JD at 00:00 UTC for a proleptic Gregorian Y/M/D,
// with no dependence on the Date object's range.
function jdFromYMD(Y, M, D) {
if (M <= 2) { Y -= 1; M += 12; }
const A = Math.floor(Y / 100);
const B = 2 - A + Math.floor(A / 4);
return Math.floor(365.25 * (Y + 4716))
+ Math.floor(30.6001 * (M + 1))
+ D + B - 1524.5;
}
jdFromYMD(2026, 7, 21); // 2461242.5
This version applies the Gregorian calendar to every date. Published
Julian Dates before October 15, 1582 use the Julian calendar; for
those, set B = 0.
Worked example: July 21, 2026 (00:00 UTC)
- Day of year: 202, so
2026202/26202 - JD Edwards: 126202
- Julian Date: 2461242.5; Julian Day Number 2461243
- Modified Julian Date: 61242
With a date library
If Luxon or Day.js is already in the project:
// Luxon: "o" is the day of year, "ooo" pads it to 3 digits
import { DateTime } from 'luxon';
DateTime.fromISO('2026-07-21').toFormat('yyyyooo'); // "2026202"
DateTime.fromFormat('2026202', 'yyyyooo', { zone: 'utc' }).toISODate(); // "2026-07-21"
// Day.js needs both the utc and dayOfYear plugins for this
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import dayOfYear from 'dayjs/plugin/dayOfYear';
dayjs.extend(utc);
dayjs.extend(dayOfYear);
dayjs.utc('2026-07-21').dayOfYear(); // 202 Related
Frequently asked questions
How do I get today's Julian date in JavaScript?
Compute the day of the year and join it to the year. For the visitor's local date: const d = new Date(); const doy = (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) - Date.UTC(d.getFullYear(), 0, 0)) / 86400000; then `${d.getFullYear()}${String(doy).padStart(3, "0")}` gives YYYYDDD. Use the getUTC methods for the UTC date instead.
Does JavaScript have a built-in day-of-year function?
The Date object does not. Temporal does: Temporal.PlainDate.from("2026-07-21").dayOfYear is 202. Luxon (DateTime.ordinal) and Day.js (with the dayOfYear plugin) also provide it.
How do I convert a Julian Date to a JavaScript Date?
new Date((jd - 2440587.5) * 86400000). The constant 2440587.5 is the Julian Date of the Unix epoch, January 1, 1970 at 00:00 UTC. For an MJD, use new Date((mjd - 40587) * 86400000).
Why is my day of year off by one?
Usually because local and UTC methods are mixed. new Date("2026-07-21") is midnight UTC, which is still July 20 in the Americas if you read it with getDate(). Use the UTC methods throughout, or the local ones throughout, and avoid daylight-saving gaps by counting days between two Date.UTC values.