Skip to main content
  1. Notes/
  2. Python 🐍/

⏰ Working with Date and Time in Python | Lesson 16

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 16: This Article

⏰ Working with Date and Time in Python
#

Dates and times are everywhereβ€”from birthday reminders to scheduling apps. Python provides powerful tools to work with dates, times, and durations easily. Let’s learn how to handle date and time like a pro! πŸ“…β³


1. The datetime Module πŸ› οΈ
#

Python’s built-in datetime module helps you create, manipulate, and format date and time.


2. Current Date and Time πŸ•’
#

from datetime import datetime

now = datetime.now()
print("Current date and time:", now)

Output looks like:

Current date and time: 2025-08-19 08:30:00.123456

3. Getting Date or Time Separately πŸ“†β°
#

print("Date:", now.date())
print("Time:", now.time())

4. Creating Specific Date or Time 🎯
#

from datetime import date, time

d = date(2025, 12, 25)
t = time(14, 30, 0)  # 2:30 PM

print("Date:", d)
print("Time:", t)

5. Formatting Date and Time (strftime) πŸ–‹οΈ
#

Convert date/time to readable strings using format codes:

CodeMeaningExample
%YYear (4 digits)2025
%mMonth (01-12)08
%dDay (01-31)19
%HHour (00-23)08
%MMinute (00-59)30
%SSecond (00-59)00

Example:

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

Output:

Formatted: 19-08-2025 08:30:00

6. Parsing Strings to Date (strptime) πŸ”„
#

Convert a date string back to a datetime object.

date_str = "25-12-2025"
date_obj = datetime.strptime(date_str, "%d-%m-%Y")
print("Parsed date:", date_obj)

7. Date Arithmetic with timedelta βž•
#

Add or subtract days, seconds, or weeks using timedelta:

from datetime import timedelta

tomorrow = now + timedelta(days=1)
print("Tomorrow:", tomorrow)

one_week_ago = now - timedelta(weeks=1)
print("One week ago:", one_week_ago)

Practice Questions with Solutions πŸ†
#

  1. Print the current date in YYYY/MM/DD format.
  2. Calculate the date 10 days from today.
  3. Convert "2025-01-01 12:00:00" string into a datetime object and print it.

Mini Project: Countdown Timer ⏳
#

Calculate the number of days remaining until a user’s next birthday.

from datetime import datetime

birthday_str = input("Enter your birthday (DD-MM): ")
birthday = datetime.strptime(birthday_str, "%d-%m")

now = datetime.now()
this_year_birthday = birthday.replace(year=now.year)

if this_year_birthday < now:
    this_year_birthday = this_year_birthday.replace(year=now.year + 1)

days_left = (this_year_birthday - now).days
print(f"Days until your birthday: {days_left}")

Checklist for This Chapter βœ…
#

  • Imported and used datetime module
  • Got current date and time
  • Formatted dates using strftime
  • Parsed strings to dates with strptime
  • Done date arithmetic using timedelta

You’re now ready to build apps that work with dates and timesβ€”like calendars, reminders, and timers! Keep experimenting! πŸŽ‰

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 16: This Article

Related

🎬 Strings in Python | Lesson 04
Python Progmramming Course
πŸ—οΈ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15
Python Progmramming Course
πŸ‘Ά Python Basics | Lesson 02
Python Progmramming Course
πŸ“„ File Handling in Python | Lesson 14
Python Progmramming Course
πŸ“‹ Lists in Python | Lesson 05
Python Progmramming Course
πŸ“– Dictionaries in Python | Lesson 07
Python Progmramming Course