Python Date and Time – Complete Guide with Examples

Introduction

Date and time are essential components of almost every software application.

Python provides powerful built-in modules for handling dates, times, timestamps, and time zones. The most commonly used module is the datetime module, which offers classes and methods to create, manipulate, format, and perform calculations with dates and times.

For example, you may need to:

  • Display the current date and time
  • Calculate a person’s age
  • Determine the number of days between two dates
  • Format dates for reports
  • Schedule tasks
  • Log application events

What is Date and Time in Python?

Date and Time in Python refer to the tools and functions used to represent and manipulate calendar dates and clock times.

Python provides several modules:

  • datetime
  • time
  • calendar

The most widely used module is datetime.

Importing the datetime Module

Before using date and time functions, import the module:


import datetime

Or:


from datetime import datetime

Getting Current Date and Time

The now() method returns the current date and time.

Example:


from datetime import datetime
current = datetime.now()
print(current)

Output:

2026-06-17 10:30:45.123456

(Output will vary.)

Getting Current Date

Use the date() method.

Example:


from datetime import datetime
today = datetime.now().date()
print(today)

Output:

2026-06-17

Getting Current Time

Use the time() method.

Example:


from datetime import datetime
current_time = datetime.now().time()
print(current_time)

Output:

10:30:45.123456

Creating a Date Object

You can create a specific date using the date class.

Syntax:


date(year, month, day)

Example:


from datetime import date
birth_date = date(2000, 5, 15)
print(birth_date)

Output:

2000-05-15

Creating a Time Object

Syntax:


time(hour, minute, second)

Example:


from datetime import time
meeting_time = time(14, 30, 0)
print(meeting_time)

Output:

14:30:00

Creating a Datetime Object

A datetime object contains both date and time.

Example:


from datetime import datetime
event = datetime(
    2026,
    12,
    25,
    10,
    30,
    0
)
print(event)

Output:

2026-12-25 10:30:00

Accessing Date Components

Example:


from datetime import datetime
today = datetime.now()
print(today.year)
print(today.month)
print(today.day)

Output:

2026
6
17

Accessing Time Components

Example:


from datetime import datetime
current = datetime.now()
print(current.hour)
print(current.minute)
print(current.second)

Output:

10
30
45

Formatting Dates with strftime()

The strftime() method converts date and time into a formatted string.

Syntax:


datetime.strftime(format)

Example:


from datetime import datetime
today = datetime.now()
print(
    today.strftime(
        "%d-%m-%Y"
    )
)

Output:

17-06-2026

Common Format Codes

Code Description
%d Day
%m Month
%Y Full Year
%y Short Year
%H Hour (24-hour)
%I Hour (12-hour)
%M Minute
%S Second
%A Full Weekday
%B Full Month

Formatting Examples

Example:


from datetime import datetime
today = datetime.now()
print(
    today.strftime(
        "%A, %d %B %Y"
    )
)

Output:

Wednesday, 17 June 2026

Converting String to Date

The strptime() method converts a string into a datetime object.

Example:


from datetime import datetime
date_string = "17-06-2026"
date_object = datetime.strptime(
    date_string,
    "%d-%m-%Y"
)
print(date_object)

Output:

2026-06-17 00:00:00

Date Arithmetic

Python allows calculations with dates.

Example:


from datetime import datetime
date1 = datetime(
    2026,
    6,
    1
)
date2 = datetime(
    2026,
    6,
    17
)
difference = date2 - date1
print(difference.days)

Output:

16

Using timedelta

The timedelta class represents a duration.

Example:


from datetime import datetime
from datetime import timedelta
today = datetime.now()
future = today + timedelta(days=10)
print(future)

Output:

Date after 10 days

Subtracting Days

Example:


from datetime import datetime
from datetime import timedelta
today = datetime.now()
previous = today - timedelta(days=30)
print(previous)

Output:

Date before 30 days

Calculating Age

Example:


from datetime import date
birth_date = date(
    2000,
    5,
    15
)
today = date.today()
age = (
    today.year -
    birth_date.year
)
print(age)

Output:

26

Working with the time Module

The time module provides functions related to timestamps and execution time.

Example:


import time
print(time.time())

Output:

1750123456.45

Represents seconds since January 1, 1970.

Pausing Program Execution

Example:


import time
print("Start")
time.sleep("3")
print("End")

Output:

Start
(wait 3 seconds)
End

Working with Calendar Module

Python’s calendar module generates calendars.

Example:


import calendar
print(
    calendar.month(
        2026,
        6
    )
)

Output:

June 2026 Calendar

Getting Weekday

Example:


from datetime import date
today = date.today()
print(
    today.weekday()
)

Output:

2

Meaning:

0 = Monday

2 = Wednesday

Comparing Dates

Example:


from datetime import date
date1 = date(
    2026,
    6,
    1
)
date2 = date(
    2026,
    6,
    17
)
print(date2 > date1)

Output:

True

Real-Life Examples:

1. Employee Attendance System


from datetime import datetime
check_in = datetime.now()
print(
    "Employee checked in:",
    check_in
)

Useful for attendance tracking.

2. Online Booking System


from datetime import datetime
from datetime import timedelta
booking = datetime.now()
expiry = booking + timedelta(days=7)
print(expiry)

Booking expires after seven days.

3. Countdown to Event


from datetime import datetime
event = datetime(
    2026,
    12,
    31
)
today = datetime.now()
remaining = event - today
print(
    remaining.days
)

Output:

Number of days remaining

4. Log Timestamp


from datetime import datetime
print(
    datetime.now().strftime(
        "%Y-%m-%d %H:%M:%S"
    )
)

Output:

2026-06-17 10:30:45

Widely used in application logging.

Advantages of Python Date and Time

Advantage Description
Easy to Use Simple API
Powerful Formatting Flexible display options
Date Arithmetic Add and subtract dates
Built-in Support No extra libraries required
Real-World Utility Useful in almost all applications

Common Mistakes

1. Forgetting to Import datetime

Incorrect:


datetime.now()

Correct:


from datetime import datetime

2. Confusing strftime and strptime

strftime() Converts:


Date → String

strptime() Converts:


String → Date

3. Using Incorrect Format Codes

Incorrect:


"%Y/%d/%m"

Correct:


"%Y/%m/%d"

4. Ignoring Time Zones

Applications used globally should consider time zone differences.

5. Calculating Age Incorrectly

Simply subtracting years may not account for birthdays that haven’t occurred yet.

Conclusion

Python Date and Time functionality is essential for handling real-world applications involving scheduling, logging, reporting, attendance systems, bookings, and data analysis.

The datetime module provides powerful tools to create, format, compare, and manipulate dates and times with ease.

Related Python Tutorials