DEV Community

still-purrfect
still-purrfect

Posted on

Date and Time in Python: Working With Real-World Moments

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

🔹 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"))
Enter fullscreen mode Exit fullscreen mode

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:

  1. Displays today’s date
  2. Asks the user for their birth year
  3. 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)