How to Get the Day of the Year from a Date in Python

If you’ve tried d.day to extract the day number of the year from a date in Python, you’ve probably seen 7 for February 7 instead of 38. That’s the day of the month.

The Python datetime day of year lives in d.timetuple().tm_yday (an int), or in d.strftime("%j") as a zero-padded string.

In this tutorial, I’ll show you how to extract the day number of the year from a date in Python, turn a day number back into a date, and do it in pandas. Every Command Prompt screenshot is a real Python 3.12.5 run.

The Shipment Dates We’ll Number

You’ll reuse five shipment dates from a Denver outdoor gear shop, saved as shipments.py:

# Shipment dates for a Denver outdoor gear shop
SHIPMENTS = ["2026-02-07", "2026-03-01", "2026-11-26",
             "2026-12-31", "2028-03-01"]

March 1 appears twice on purpose: 2028 is a leap year, so it lands one day later. Dates stored as text? Convert the string to a date in Python first.

Method 1: Get the Day of the Year with Python datetime timetuple() (Recommended)

timetuple() returns a time.struct_time, and its tm_yday field is the day number from 1 to 366. Here’s the code on the same five dates:

from datetime import date
from shipments import SHIPMENTS

for text in SHIPMENTS:
    d = date.fromisoformat(text)
    yday = d.timetuple().tm_yday
    print(f"{d:%b %d, %Y}  day of month {d.day:>2}  day of year {yday}")

print("Today is day", date.today().timetuple().tm_yday)

Output:

Feb 07, 2026  day of month  7  day of year 38
Mar 01, 2026  day of month  1  day of year 60
Nov 26, 2026  day of month 26  day of year 330
Dec 31, 2026  day of month 31  day of year 365
Mar 01, 2028  day of month  1  day of year 61
Today is day 331
Python datetime day of year: Command Prompt output of tm_yday next to the day of month for five shipment dates
February 7 is day 38, and March 1 is day 60 in 2026 but day 61 in 2028.

Thanksgiving (November 26) is day 330, December 31 is day 365, and the last line printed 331 for the current date in Python.

How does this code work?

d.day is the day of the month, and d.year the year. tm_yday counts from January 1, so leap years are handled for you; the same field works on a datetime.

Method 2: Get the Day Number as Text with strftime(“%j”)

Need the day number for a file name or a batch label? Use the %j format code. Same February 7 shipment, read from a US-style string:

from datetime import datetime

d = datetime.strptime("02/07/2026", "%m/%d/%Y")
print(repr(d.strftime("%j")))           # zero-padded string
print(int(d.strftime("%j")))            # number for math
print(f"Batch {d:%Y-%j}")               # label like 2026-038
print(repr(f"{datetime(2026, 1, 1):%j}"))

Output:

'038'
38
Batch 2026-038
'001'
Command Prompt output of Python strftime %j returning the day of year 038 as a string and 38 as an int
%j returns the string ‘038’, int() turns it into 38, and January 1 prints as ‘001’.

How does this code work?

%j always gives three digits, which keeps labels like 2026-038 sortable. Wrap it in int() before math; see converting a string to an integer in Python.

How Do You Convert a Day of the Year Back to a Date in Python?

Going from a day number to a date is common with satellite data and logs. Here’s the code, including a trap I hit with day 366:

from datetime import date, datetime, timedelta

print(date(2026, 1, 1) + timedelta(days=330 - 1))
print(datetime.strptime("2026-366", "%Y-%j").date())  # no error!


def from_day_of_year(year, n):
    d = date(year, 1, 1) + timedelta(days=n - 1)
    if d.year != year:
        raise ValueError(f"{year} has no day {n}")
    return d


print(from_day_of_year(2028, 366))
try:
    from_day_of_year(2026, 366)
except ValueError as e:
    print("ValueError:", e)

Output:

2026-11-26
2027-01-01
2028-12-31
ValueError: 2026 has no day 366
Command Prompt output converting Python day of year 330 back to a date and strptime %j rolling 2026 day 366 into 2027
Day 330 of 2026 is November 26, but strptime turns day 366 of 2026 into January 1, 2027 without an error.

How does this code work?

Adding n - 1 days to January 1 gives day n. strptime("%Y-%j") parses the same thing, but it quietly rolls day 366 into the next year.

Important: from_day_of_year() checks the year it landed in, so 2026 day 366 raises a ValueError. To move a date by days, see subtracting days from a date in Python.

Method 3: Get the Day of the Year in pandas

For a CSV or Excel column, use pandas 3.0.6. Same five dates, now a DataFrame column:

import pandas as pd
from shipments import SHIPMENTS

df = pd.DataFrame({"shipped": pd.to_datetime(SHIPMENTS)})
df["day_of_year"] = df["shipped"].dt.day_of_year
df["leap_year"] = df["shipped"].dt.is_leap_year
print(df)

Output:

     shipped  day_of_year  leap_year
0 2026-02-07           38      False
1 2026-03-01           60      False
2 2026-11-26          330      False
3 2026-12-31          365      False
4 2028-03-01           61       True
Command Prompt output of a pandas DataFrame with dt.day_of_year and dt.is_leap_year columns for the shipment dates
The 2028 row gets day 61 and is the only one flagged as a leap year.

How does this code work?

dt.day_of_year (alias dt.dayofyear) returns integers. For text dates, convert the pandas column to datetime first.

Picking the Right Way to Get the Day of the Year in Python

MethodReturnsFeb 7, 2026Best for
d.timetuple().tm_ydayint38Most code
d.strftime("%j")str“038”Labels, file names
Series.dt.day_of_yearint column38pandas data

In my own Python scripts I use tm_yday unless I need a padded label.

Day-of-Year Gotchas I Hit in Python

Python day numbers tripped me up in three places:

  • Leap years: every date after February 28 shifts by one; check for a leap year in Python before comparing years.
  • Day 366: strptime rolls it into the next year instead of failing.
  • Strings: %j returns '038', not 38.

With day numbers sorted, these date tutorials fit next:

Questions About the Day of the Year in Python

How do I get the day of the year in Python?

Use d.timetuple().tm_yday; for February 7, 2026 it returns 38.

How do I get today’s day of the year?

Use date.today().timetuple().tm_yday.

What does %j mean in Python strftime?

%j is the day of the year as a zero-padded three-digit string, from 001 to 366.

How do I convert a day of the year to a date in Python?

Add n - 1 days to January 1: date(2026, 1, 1) + timedelta(days=329) gives November 26, 2026.

Does tm_yday handle leap years?

Yes. March 1 is day 60 in 2026 and day 61 in 2028, and December 31 can be day 366.

Where is the %j format code documented?