Code · Python
Julian dates in Python
Standard-library code for the day-of-year Julian date (YYYYDDD, YYDDD, YDDD and JD Edwards CYYDDD) in both directions, the same work on pandas columns, and a tested function for the astronomical Julian Date (JD) and Modified Julian Date (MJD), with its inverse.
Two different “Julian dates”
The ordinal form is a year plus the day of the year,
like 2026202. It is used on labels, in ERP systems and on
mainframes. The astronomical form is a running count
of days since noon UTC on January 1, 4713 BC, like
2461242.5. Want to check a value? Use the
Julian date converter.
Day-of-year (ordinal) Julian dates
The day of the year (1–366) is on the time tuple:
>>> from datetime import date, datetime
>>> date(2026, 7, 21).timetuple().tm_yday
202
For the codes, use strftime. %j is the
zero-padded three-digit day of the year, %Y the four-digit
year and %y the two-digit year:
>>> d = date(2026, 7, 21)
>>> d.strftime('%Y%j') # 7-digit YYYYDDD
'2026202'
>>> d.strftime('%y%j') # 5-digit YYDDD
'26202'
>>> int(d.strftime('%Y%j')) # as a number
2026202 Today’s Julian date depends on the time zone, because the date changes at a different moment in each one:
from datetime import datetime, timezone
datetime.now().strftime('%Y%j') # today in the computer's time zone
datetime.now(timezone.utc).strftime('%Y%j') # today in UTC Parse a code back to a date
strptime takes the same directives:
>>> datetime.strptime('2026202', '%Y%j').date()
datetime.date(2026, 7, 21)
>>> datetime.strptime('26202', '%y%j').date()
datetime.date(2026, 7, 21)
Codes often arrive as numbers, from a CSV or a database, and a 5-digit
code like 05001 may already have lost its leading zero.
Integer arithmetic handles every case and lets you choose the century
rule:
from datetime import date, timedelta
def from_yyyyddd(code):
"""2026202 or '2026202' -> date(2026, 7, 21)"""
code = int(code)
return date(code // 1000, 1, 1) + timedelta(days=code % 1000 - 1)
def from_yyddd(code, pivot=68):
"""26202 -> 2026-07-21. Also works when the leading zero is lost (5001)."""
yy, ddd = divmod(int(code), 1000)
year = 2000 + yy if yy <= pivot else 1900 + yy
return date(year, 1, 1) + timedelta(days=ddd - 1)
def from_yddd(code, ref=None):
"""4-digit YDDD (MRE cases, military documents): the latest matching
date that is not after ref (default: today)."""
ref = ref or date.today()
y, ddd = divmod(int(code), 1000)
year = ref.year - (ref.year - y) % 10
result = date(year, 1, 1) + timedelta(days=ddd - 1)
if result > ref:
result = date(year - 10, 1, 1) + timedelta(days=ddd - 1)
return result >>> from_yyyyddd(2024366)
datetime.date(2024, 12, 31)
>>> from_yyddd(5001)
datetime.date(2005, 1, 1)
>>> from_yddd(6202, ref=date(2026, 11, 17))
datetime.date(2026, 7, 21) Two-digit years are a guess
%y maps 00–68 to 2000–2068 and 69–99 to
1969–1999. That is a convention, not information in the code.
If your data spans another range, set pivot yourself, and
store new records as YYYYDDD.
JD Edwards (CYYDDD) dates
Oracle JD Edwards stores dates as CYYDDD, where C counts centuries since
1900. Values for the 1900s come back as five digits (99365).
See the JDE converter for the
details.
def to_jde(d):
"""date(2026, 7, 21) -> 126202"""
return (d.year - 1900) * 1000 + d.timetuple().tm_yday
def from_jde(jde):
"""126202 or '099365' -> date. JDE stores 'no date' as 0."""
jde = int(jde)
if jde == 0:
return None
return date(1900 + jde // 1000, 1, 1) + timedelta(days=jde % 1000 - 1) pandas
The .dt accessor does the same work on a whole column:
import pandas as pd
df = pd.DataFrame({'date': pd.to_datetime(['2026-07-21', '2005-01-05'])})
df['doy'] = df['date'].dt.day_of_year # 202, 5
df['yyyyddd'] = df['date'].dt.strftime('%Y%j') # '2026202', '2005005'
df['yyddd'] = df['date'].dt.strftime('%y%j') # '26202', '05005'
df['jde'] = (df['date'].dt.year - 1900) * 1000 + df['date'].dt.day_of_year # 126202, 105005
To parse codes, convert them to strings and pass a format. Pad YYDDD
values with zfill(5) first, because a numeric column has
already dropped the leading zeros:
codes = pd.Series([2026202, 2024366])
pd.to_datetime(codes.astype(str), format='%Y%j') # 2026-07-21, 2024-12-31
short = pd.Series([26202, 5005, 99365]) # YYDDD read as numbers
pd.to_datetime(short.astype(str).str.zfill(5), format='%y%j') # 2026-07-21, 2005-01-05, 1999-12-31
jde = pd.Series([126202, 99365])
pd.to_datetime((jde + 1900000).astype(str), format='%Y%j') # 2026-07-21, 1999-12-31 pandas also has the astronomical Julian Date built in:
pd.Timestamp('2026-07-21').to_julian_date() # 2461242.5
pd.to_datetime(2461242.5, unit='D', origin='julian') # Timestamp('2026-07-21 00:00:00')
pd.to_datetime(61242.75, unit='D', origin=pd.Timestamp('1858-11-17')) # MJD -> 2026-07-21 18:00
Older pandas versions call the accessor .dt.dayofyear;
pandas 3 still accepts it but warns that it will be removed.
Astronomical Julian Date (JD) and MJD
The datetime module has no JD function, so use the standard
algorithm from Jean Meeus’s Astronomical Algorithms. This
version keeps the time of day down to the microsecond, converts
time-zone-aware datetimes to UTC, and switches from the Gregorian to the
Julian calendar before October 15, 1582, as astronomers do:
import math
from datetime import datetime, timedelta, timezone
def to_jd(dt):
"""Astronomical Julian Date for a datetime. Naive datetimes are taken as UTC.
Gregorian calendar from 1582-10-15, Julian calendar before (Meeus, ch. 7)."""
if dt.tzinfo is not None:
dt = dt.astimezone(timezone.utc).replace(tzinfo=None)
y, m = dt.year, dt.month
d = dt.day + (dt.hour + (dt.minute + (dt.second + dt.microsecond / 1e6) / 60) / 60) / 24
if m <= 2:
y -= 1
m += 12
if (dt.year, dt.month, dt.day) >= (1582, 10, 15):
a = y // 100
b = 2 - a + a // 4
else:
b = 0
return math.floor(365.25 * (y + 4716)) + math.floor(30.6001 * (m + 1)) + d + b - 1524.5
def to_mjd(dt):
return to_jd(dt) - 2400000.5
def from_jd(jd):
"""Naive UTC datetime for a Julian Date (Meeus, ch. 7)."""
z = math.floor(jd + 0.5)
f = jd + 0.5 - z
if z >= 2299161:
alpha = math.floor((z - 1867216.25) / 36524.25)
a = z + 1 + alpha - alpha // 4
else:
a = z
b = a + 1524
c = math.floor((b - 122.1) / 365.25)
d = math.floor(365.25 * c)
e = math.floor((b - d) / 30.6001)
day = b - d - math.floor(30.6001 * e)
month = e - 1 if e < 14 else e - 13
year = c - 4716 if month > 2 else c - 4715
return datetime(year, month, day) + timedelta(days=f) >>> to_jd(datetime(2026, 7, 21))
2461242.5
>>> to_jd(datetime(2026, 7, 21, 12))
2461243.0
>>> to_mjd(datetime(2026, 7, 21))
61242.0
>>> to_jd(datetime(1582, 10, 4)) # last day of the Julian calendar
2299159.5
>>> from_jd(2461243.25)
datetime.datetime(2026, 7, 21, 18, 0)
>>> from_jd(61242.0 + 2400000.5) # MJD -> date
datetime.datetime(2026, 7, 21, 0, 0)
The JD day starts at noon UTC, so midnight ends in
.5. The MJD (JD − 2400000.5) starts at midnight.
For the current values:
>>> to_jd(datetime.now(timezone.utc)) # JD right now
>>> math.floor(to_jd(datetime.now(timezone.utc)) + 0.5) # today's Julian Day Number (UTC date) Shortcut for whole dates
date.toordinal() counts days from January 1 of year 1,
so a fixed offset turns it into a JD or a Julian Day Number:
>>> d = date(2026, 7, 21)
>>> d.toordinal() + 1721424.5 # JD at 00:00 UTC
2461242.5
>>> d.toordinal() + 1721425 # Julian Day Number
2461243
>>> date.fromordinal(2461243 - 1721425)
datetime.date(2026, 7, 21) Before 1582 the two methods disagree
toordinal() applies the Gregorian calendar to every date,
even before it existed. to_jd above uses the Julian
calendar before October 15, 1582, which is the convention behind
published Julian Dates. For dates after 1582 they agree exactly.
With astropy
For precision astronomy, astropy.time handles time scales
(UTC, TT, TDB), leap seconds and dozens of formats:
from astropy.time import Time
>>> Time('2026-07-21 00:00:00', scale='utc').jd
2461242.5
>>> Time(2461242.5, format='jd', scale='utc').iso
'2026-07-21 00:00:00.000'
>>> Time(2461242.5, format='jd', scale='utc').mjd
61242.0
>>> Time('2026-07-21 00:00:00', scale='utc').tt.jd # same instant in Terrestrial Time
2461242.5008007... Worked example: July 21, 2026
| Value | Code | Result |
|---|---|---|
| Day of year | d.timetuple().tm_yday | 202 |
| YYYYDDD | d.strftime('%Y%j') | 2026202 |
| YYDDD | d.strftime('%y%j') | 26202 |
| JDE CYYDDD | to_jde(d) | 126202 |
| JD at 00:00 UTC | to_jd(datetime(2026, 7, 21)) | 2461242.5 |
| Julian Day Number | d.toordinal() + 1721425 | 2461243 |
| MJD | to_mjd(datetime(2026, 7, 21)) | 61242.0 |
Related
The same conversions in JavaScript, SQL and Excel, or check a value with the Julian day converter.
Frequently asked questions
How do I get the Julian date in Python?
For the day-of-year form, use strftime: date.today().strftime("%Y%j") gives YYYYDDD (2026202 for July 21, 2026) and "%y%j" gives YYDDD. For the astronomical Julian Date, use the to_jd function on this page, pandas Timestamp.to_julian_date(), or astropy.time.Time(...).jd.
How do I convert a Julian date string to a date in Python?
datetime.strptime("2026202", "%Y%j").date() returns date(2026, 7, 21), and "%y%j" parses a 5-digit YYDDD code. If the code is a number that may have lost its leading zero, split it with divmod(code, 1000) instead, as in from_yyddd on this page.
What does %j mean in Python?
%j is the day of the year as a zero-padded three-digit number, 001 to 366. It works in both strftime (formatting) and strptime (parsing).
How does Python read two-digit years with %y?
Values 00 to 68 become 2000 to 2068 and 69 to 99 become 1969 to 1999. If your data needs a different cutoff, parse the year yourself.
How do I convert a Julian Date (JD) to a datetime in Python?
Use from_jd on this page, pd.to_datetime(jd, unit="D", origin="julian") in pandas, or Time(jd, format="jd").datetime in astropy. For an MJD, add 2400000.5 first.