Python Working with Dates Example

Python can work with dates and times using the datetime module. In this example, you will learn how to do common date tasks with small, clear examples.

You will see how to:

  • Get the current date
  • Get the current date and time
  • Format dates as readable text
  • Convert a string into a date
  • Compare dates
  • Find the number of days between dates

Quick example

from datetime import datetime, date

today = date.today()
print(today)

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

Use date.today() for the current date and datetime.now() when you also need the current time.


What this example covers

  • Show how to get the current date
  • Show how to get the current date and time
  • Show how to format dates as readable strings
  • Show how to convert text into a date
  • Show how to compare two dates

When to use date handling in Python

Date handling is useful when you need to:

  • Display today's date in a program
  • Timestamp files or logs
  • Check deadlines or due dates
  • Calculate how many days are between two dates
  • Convert user input like 2025-04-22 into a date object

Main tools used in this example

This page uses a few important parts of the datetime module:

  • datetime.date for dates only
  • datetime.datetime for date and time together
  • datetime.now() to get the current date and time
  • date.today() to get the current date
  • strftime() to format a date as text
  • strptime() to parse text into a date

If you want a broader overview, see the Python datetime module overview.

Step 1: Get the current date

Import date from datetime, then call date.today().

from datetime import date

today = date.today()
print(today)

Example output:

2026-04-22

By default, Python prints dates in year-month-day format.

Step 2: Get the current date and time

If you also need the time, use datetime.now().

from datetime import datetime

now = datetime.now()
print(now)

Example output:

2026-04-22 14:35:10.123456

This includes:

  • Year
  • Month
  • Day
  • Hour
  • Minute
  • Second
  • Microseconds

For a focused explanation, see datetime.now() explained.

Step 3: Format a date for display

Use strftime() to turn a date or datetime object into a string in the format you want.

from datetime import date

today = date.today()

print(today.strftime("%d-%m-%Y"))
print(today.strftime("%Y/%m/%d"))

Example output:

22-04-2026
2026/04/22

Common format codes:

  • %Y = 4-digit year
  • %m = month as a number
  • %d = day of month
  • %H = hour
  • %M = minute
  • %S = second

Important: strftime() changes how the value is displayed. It does not change the original date object.

You can learn more in datetime.strftime() explained.

Step 4: Convert a string into a date

Use datetime.strptime() when you have text and want to turn it into a date value.

from datetime import datetime

text = "2026-04-22"
parsed_date = datetime.strptime(text, "%Y-%m-%d").date()

print(parsed_date)
print(type(parsed_date))

Output:

2026-04-22
<class 'datetime.date'>

The format string must match the input text exactly:

  • "2026-04-22" matches "%Y-%m-%d"
  • "22/04/2026" would need "%d/%m/%Y"

If the format does not match, Python raises a ValueError. If that happens, see ValueError in Python: causes and fixes.

You can also read datetime.strptime() explained.

Step 5: Compare dates

You can compare date objects with <, >, and ==.

from datetime import date

start_date = date(2026, 4, 20)
deadline = date(2026, 4, 25)

print(start_date < deadline)
print(start_date == deadline)

if start_date < deadline:
    print("The start date is earlier than the deadline.")

Output:

True
False
The start date is earlier than the deadline.

Earlier dates are smaller than later dates.

Make sure both values are date objects. Do not compare a plain string like "2026-04-22" with a date object.

Step 6: Find the difference between dates

You can subtract one date from another. The result is a timedelta object.

from datetime import date

today = date(2026, 4, 22)
due_date = date(2026, 5, 1)

difference = due_date - today

print(difference)
print(difference.days)

Output:

9 days, 0:00:00
9

Use .days when you want just the number of days.

Here is a simple countdown example:

from datetime import date

today = date.today()
deadline = date(2026, 5, 1)

days_left = (deadline - today).days

print(f"Days left: {days_left}")

Common beginner mistakes

Here are some common problems when working with dates in Python:

  • Forgetting to import date or datetime
  • Using the wrong strptime() format string
  • Mixing strings and date objects in comparisons
  • Calling datetime.today without parentheses
  • Expecting formatted output without using strftime()

Common causes include:

  • Using a date string without converting it first
  • Wrong format code in strptime()
  • Confusing date with datetime
  • Trying to compare text values instead of date objects
  • Forgetting that month and day order must match the format string

If something is not working, these simple checks can help:

python your_script.py
print(type(today))
print(type(user_input))
print(parsed_date)
print(parsed_date.strftime('%Y-%m-%d'))

These checks help you confirm:

  • Whether a value is a string or a date object
  • Whether parsing worked correctly
  • Whether your formatted output looks the way you expect

Next steps

After this example, a good next step is to practice real tasks such as:

  • Adding timestamps to logs
  • Checking whether a deadline has passed
  • Parsing user-entered dates
  • Building a small due-date checker script

You may also want to learn:

FAQ

What is the difference between date and datetime in Python?

date stores only the year, month, and day. datetime stores the date and the time.

How do I print a date in a different format?

Use strftime() with format codes like %Y, %m, and %d.

How do I turn a string into a date?

Use datetime.strptime() and make sure the format string matches the input text.

How do I calculate the number of days between two dates?

Subtract one date from another and use the .days value from the result.

Why does parsing a date string fail?

Usually the input text does not match the format string exactly.

See also