Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. datetime.datetime() / datetime.date() / datetime.time()

datetime.datetime() / datetime.date() / datetime.time()

Functions from the datetime module, which creates objects for working with dates and times. These are the building blocks for manipulating dates, times, and datetimes in your programs.

Syntax

from datetime import datetime, date, time

# Creates a datetime object.
dt = datetime(year, month, day, hour, minute, second)

# Creates a date object.
d = date(year, month, day)

# Creates a time object.
t = time(hour, minute, second)

# Gets the current date and time.
now = datetime.now()

# Gets today's date.
today = date.today()

Function List

Function / MethodDescription
datetime(year, month, day, hour, minute, second)Creates a datetime object for the specified date and time. Hour, minute, and second default to 0 if omitted.
date(year, month, day)Creates a date object for the specified date.
time(hour, minute, second)Creates a time object for the specified time.
datetime.now()Returns a datetime object representing the current local date and time.
datetime.utcnow()Returns a datetime object representing the current UTC date and time.
date.today()Returns a date object representing today's date.
datetime.fromisoformat(string)Creates a datetime object from an ISO 8601 formatted string (e.g., '2025-04-15T12:00:00').

Sample Code

from datetime import datetime, date, time

# Gets the current date and time.
now = datetime.now()
print(now)  # Example output: '2025-04-15 14:30:00.123456'

# Gets today's date only.
today = date.today()
print(today)  # Example output: '2025-04-15'

# Creates a datetime object for a specific date and time.
dt = datetime(2025, 4, 15, 9, 30, 0)
print(dt)  # Output: '2025-04-15 09:30:00'

# Creates a date object.
d = date(2025, 12, 31)
print(d)          # Output: '2025-12-31'
print(d.year)     # Output: '2025'
print(d.month)    # Output: '12'
print(d.day)      # Output: '31'

# Creates a time object.
t = time(23, 59, 59)
print(t)  # Output: '23:59:59'

# Accesses individual attributes of a datetime object.
dt2 = datetime.now()
print(dt2.year)      # Gets the year.
print(dt2.month)     # Gets the month.
print(dt2.weekday()) # Gets the day of the week (0=Monday to 6=Sunday).

# Creates a datetime from an ISO format string.
dt3 = datetime.fromisoformat('2025-04-15T09:30:00')
print(dt3)  # Output: '2025-04-15 09:30:00'

# Converts a datetime to a date.
d2 = datetime.now().date()
print(type(d2))  # Output: '<class 'datetime.date'>'

Notes

Python's datetime module contains several classes. datetime.datetime holds both a date and a time, datetime.date holds only a date, and datetime.time holds only a time. Writing from datetime import datetime can be confusing because the class name matches the module name, but this is the conventional way to import it.

When getting the current time, the value returned by datetime.now() depends on the server's timezone setting. If you need to handle timezones explicitly, use the standard zoneinfo module (Python 3.9+) or the pytz library for earlier versions.

For formatting and parsing datetime strings, see datetime.strftime() / datetime.strptime(). For calculating time intervals, see datetime.timedelta().

If you find any errors or copyright issues, please .