πŸ”· Introduction

Handling dates and time is essential in many real-world applications such as scheduling systems, logs, data analysis, and APIs.

Python provides a powerful built-in module called datetime to work with dates and time efficiently.

In this lesson, you will learn:

βœ” How to create and manipulate dates
βœ” How to format and parse date strings
βœ” How to perform time calculations
βœ” How to work with durations using timedelta

🟩 1. The datetime Module

πŸ”Ή Importing

from datetime import datetime

πŸ”Ή Get current date and time

now = datetime.now()
print(now)

πŸ”Ή Get only the date

print(now.date())

πŸ”Ή Get only the time

print(now.time())

🟩 2. Creating Custom Dates

from datetime import datetime

d = datetime(2025, 1, 15, 10, 30)
print(d)

βœ” Format: datetime(year, month, day, hour, minute)

🟩 3. Formatting Dates (strftime)

Convert date β†’ string:

now = datetime.now()

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

πŸ”Ή Common format codes

CodeMeaning
%YYear
%mMonth
%dDay
%HHour
%MMinute
%SSecond

🟩 4. Parsing Dates (strptime)

Convert string β†’ date:

from datetime import datetime

date_str = "2025-01-15"
date_obj = datetime.strptime(date_str, "%Y-%m-%d")

print(date_obj)

🟩 5. Time Calculations (timedelta)

πŸ”Ή Adding time

from datetime import timedelta

now = datetime.now()
future = now + timedelta(days=7)

print(future)

πŸ”Ή Subtracting dates

past = datetime(2024, 1, 1)
diff = now - past

print(diff.days)

🟩 6. Practical Examples

πŸ”Ή Calculate age

birth = datetime(2000, 5, 10)
today = datetime.now()

age = today.year - birth.year
print("Age:", age)

πŸ”Ή Check if a date is in the future

event = datetime(2025, 12, 31)

if event > datetime.now():
    print("Event is upcoming")

🟧 7. Exercises (Hidden Solutions)

Exercise 1 β€” Print current date

Exercise 2 β€” Format today’s date

Exercise 3 β€” Parse a date string

Exercise 4 β€” Add 10 days to today

Exercise 5 β€” Calculate difference between two dates

🟦 Conclusion

In this lesson, you learned how to:

βœ” Work with dates and time using datetime
βœ” Format and parse date strings
βœ” Perform time calculations
βœ” Handle real-world date scenarios

These skills are widely used in:

πŸš€ Mini Project β€” Event Scheduler & Countdown App

Project idea

Build a Python program that lets the user:

This project is perfect for practicing:

🧩 What the app does

Example:

Enter event name: Python Exam
Enter event date (YYYY-MM-DD): 2026-12-20

Output:

Event: Python Exam
Date: 20/12/2026
Days remaining: 45

If the date is today:

The event is today!

If the date is in the past:

This event has already passed.

βœ… Full Solution

🧠 Skills practiced

With this mini project, you practice:

πŸ›  Improved Version β€” Multiple Events

Here is a slightly more advanced version that stores several events in a list.

🎯 Challenge ideas

You can extend the project with:

  1. sort events by nearest date
  2. save events to a file
  3. show only future events
  4. add event time
  5. create a GUI version with Tkinter
  6. highlight the nearest upcoming event