← Back to Chapters

Python datetime Module

? Python datetime Module

⚡ Quick Overview

The datetime module in Python lets you work with dates, times, and combined date-time values. You can:

  • Get the current date and time.
  • Create specific date, time, or datetime objects.
  • Add or subtract time using timedelta.
  • Format datetime objects as strings and parse strings back to datetime.

? Key Concepts

  • date – only the calendar date (year, month, day).
  • time – only the time of day (hour, minute, second, microsecond).
  • datetime – combination of date and time.
  • timedelta – difference between two dates/times.
  • strftime() – format a datetime as a string.
  • strptime() – parse a string into a datetime.

? Syntax and Components

Common ways to use the datetime module:

  • import datetime – import the whole module.
  • datetime.date(year, month, day) – create a date.
  • datetime.time(hour, minute, second) – create a time.
  • datetime.datetime(year, month, day, hour, minute, second) – create a full datetime.
  • datetime.date.today() – get today’s date.
  • datetime.datetime.now() – get current date and time.
  • datetime.timedelta(...) – represent a duration for arithmetic.

? Code Examples

? Importing the datetime Module

The datetime module allows you to work with dates and times in Python.

? View Code Example
import datetime
print(datetime.datetime.now())  # Current date and time

? Getting Current Date and Time

? View Code Example
import datetime

today = datetime.date.today()
print(today)                 # YYYY-MM-DD

now = datetime.datetime.now()
print(now)                   # YYYY-MM-DD HH:MM:SS.mmmmmm

? Creating Specific Date and Time

? View Code Example
import datetime

my_date = datetime.date(2025, 9, 11)
my_time = datetime.time(14, 30, 0)
my_datetime = datetime.datetime(2025, 9, 11, 14, 30, 0)

print(my_date)       # Date: 2025-09-11
print(my_time)       # Time: 14:30:00
print(my_datetime)   # Full datetime object

? Date and Time Arithmetic

? View Code Example
import datetime
from datetime import timedelta

delta = timedelta(days=5, hours=3)                     # 5 days and 3 hours duration
future_date = datetime.datetime.now() + delta          # Date-time in the future
past_date = datetime.datetime.now() - delta            # Date-time in the past

print(future_date)                                     # Future date and time
print(past_date)                                       # Past date and time

? Formatting Dates and Times

? View Code Example
import datetime

now = datetime.datetime.now()
formatted = now.strftime("%A, %d %B %Y %I:%M %p")
print(formatted)   # e.g., Thursday, 11 September 2025 02:30 PM

⏱ Parsing Strings to datetime

? View Code Example
import datetime

date_str = "2025-09-11 14:30:00"                                # Input date-time as string
parsed_date = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")   # Convert string to datetime
print(parsed_date)                                               # Parsed datetime object

? Live Output and Explanation

? What the Outputs Look Like

  • datetime.date.today() might print something like 2025-09-11.
  • datetime.datetime.now() might print 2025-09-11 14:30:15.123456.
  • strftime("%A, %d %B %Y %I:%M %p") could give Thursday, 11 September 2025 02:30 PM.
  • timedelta arithmetic changes the date/time by the specified amount (days, hours, etc.).
  • strptime() converts a properly formatted string into a real datetime object you can use in calculations.

✅ Tips

  • Use datetime.datetime.now() for current date and time.
  • Use strftime() to format datetime objects as strings.
  • Use strptime() to parse strings into datetime objects.
  • timedelta helps in date arithmetic (adding or subtracting time).

? Practice Tasks

  • Get the current date and time and format it as "DD/MM/YYYY HH:MM".
  • Create a datetime object for your next birthday.
  • Add 10 days and 5 hours to the current date and print the result.
  • Parse the string "2025-12-25 10:00:00" to a datetime object using strptime().