API · Python

Python date.day Attribute

Learn how to read the day attribute from a Python datetime.date, use it with a specific or current date, and handle invalid dates.

On this page

To get the day of the month in Python, construct a datetime.date and read its day attribute, for example some_date.day. The attribute returns an integer from 1 through 31; it is not called with parentheses. You can use the same attribute on datetime.date.today() to get the day for the current local date.

What date.day Returns

In Python, datetime.date represents a calendar date, and date.day is an attribute on a date instance. It gives the day of the month as an integer from 1 through 31. Read the attribute without parentheses: some_date.day is correct, while some_date.day() attempts to call an integer as a function. The result is only the day number; it does not return a new date or format a date string. For example, the day attribute of April 13 is 13.

A date must already exist before you can read its day. Construct a datetime.date with a year, month, and day, then access .day on the returned object. The year range is 1 through 9999, the month range is 1 through 12, and the permitted day depends on that particular month and year. Months have different lengths, and February has 29 days in leap years. The attribute reports the supplied calendar day; it does not add leading zeros or change how integer literals are written.

Access the Day of a Date

Run this example with a specific date to retrieve its day number. In the supplied run, it printed 13.

import datetime

chosen_date = datetime.date(2019, 4, 13)
print(chosen_date.day)
13

This program reads the date instance’s integer day attribute. If you need a complete date string instead, print the date object itself; that is a different result from reading its day attribute. Keep the distinction in mind when passing values to other code: .day is an integer, so it can be compared or used in arithmetic as a number, but it does not retain the month or year. Store or pass the original date object whenever the full calendar date is needed.

Get Today’s Day of the Month

Call datetime.date.today() to obtain the current local date, then read its day attribute. This run printed 6; the value changes with the current local date, so future runs can print a different number.

import datetime

today = datetime.date.today()
print(today.day)
6

This is useful when a program needs the current day of the month, such as displaying a calendar date. It does not return the day of the week. For that, use a weekday-related method on the date object. Because today() uses the machine’s local date, systems near midnight in different time zones may report different dates.

Handle Invalid Dates

A date constructor checks whether its components form a real calendar date. An out-of-range year, month, or day is rejected with ValueError; for example, year 20000 is outside the supported range. In the supplied run, the example caught the ValueError and printed its type name, ValueError.

import datetime

try:
    invalid_date = datetime.date(20000, 1, 9)
except ValueError as error:
    error_type = type(error).__name__
    print(error_type)
ValueError

The constructor fails before Python can read .day, so the final line is not reached for this input. A day such as 31 is valid in some months but not in all of them, and February’s valid final day depends on whether the year is a leap year. Validate or parse the complete date rather than assuming that every number from 1 to 31 is valid for every month. For application input, catch ValueError around date construction when you want to report a friendly message or ask for another date. Once construction succeeds, the resulting date.day value is always within 1 through 31.