Every app you use alarms, calendars and reminders depends on one thing:
Time.
So far, we’ve learned how to write programs that store data, make decisions, handle errors, and work with files.
Now we move into something more practical: date and time.
Python allows us to work with time using the built-in datetime module.
🔹 Getting the Current Date and Time
import datetime
now = datetime.datetime.now()
print(now)
This prints the current date and time from your system.
🔹 Working with Dates Only
Sometimes you only need the date.
import datetime
today = datetime.date.today()
print(today)
This gives only the current date.
🔹 Creating Your Own Date
You can also create a specific date.
import datetime
birthday = datetime.date(2000, 5, 17)
print(birthday)
🔹 Formatting Date and Time
Raw date formats can look messy, so we format them.
import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d"))
This gives a clean format like:
2026-06-08
💡 Why Date and Time Matter
Date and time are used in:
- Reminders ⏰
- Scheduling apps 📅
- Login systems 🔐
- Logs in applications 📊
- Automated tasks 🤖 Almost every real system tracks time in some way. Without it, apps would feel static and unorganized.
🌱 Challenge
Write a program that:
- Displays today’s date
- Asks the user for their birth year
- Calculates their age Bonus: Format the output in a clean sentence.
Next, we’ll explore sets, where you learn how to store unique values and remove duplicates automatically.
Top comments (0)