Python Dates

Work with dates and times.

✍️ 3 exercises + code challenge in this lesson

The datetime module handles dates and times.

  • datetime.now(): the current date and time.
  • date(2026, 12, 31): a specific date.
  • timedelta(days=7): a duration you can add or subtract.
  • strftime("%d %B %Y"): format as text. strptime() parses text.

Common codes: %Y year, %m month, %d day, %B month name, %A weekday, %H:%M time.

Key Takeaways: Python Dates

Mastering Python Dates helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python Dates Code Example

Python
from datetime import datetime, date, timedelta

now = datetime.now()
print("Now:", now.strftime("%A, %d %B %Y %H:%M"))

new_year = date(now.year + 1, 1, 1)
print("Days until new year:", (new_year - now.date()).days)

deadline = now + timedelta(days=14)
print("Deadline:", deadline.strftime("%d/%m/%Y"))

parsed = datetime.strptime("22-10-2026", "%d-%m-%Y")
print("Parsed weekday:", parsed.strftime("%A"))
Free sign-in required · Google login available

Python Dates Practice Exercises

Check what you learned in “Python Dates”.

/ 3 solved
Fill in the blank

Format the date as day/month/year.

today.strftime("")
✓ Correct! Not quite. Try again.
Multiple choice

Which class represents a duration like 7 days?

✓ Correct! Not quite. Try again.
Code challenge

Create date(2026, 12, 25) and print it with strftime("%d %B %Y").

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services