Python Datetime Module

📅 Python Datetime Module 

Working with dates and times is an essential part of programming. In Python, the datetime module provides classes for manipulating dates and times easily. Whether you need to log timestamps, format dates for reports, or calculate differences between dates, datetime makes it possible. In this guide, we will explore Python Dates, Date Output, Creating Date Objects, the strftime() Method, and all legal format codes.

⏰ What is Python Datetime?

The datetime module in Python supplies classes for working with date and time. It allows you to handle timestamps, format date output, and perform arithmetic operations on dates and times. By default, Python doesn’t include a datatype for date/time, so this module is very important.

Syntax:
import datetime

📌 Python Dates

A date in Python is not a standalone data type. Instead, you use the datetime module to work with them. The datetime class has attributes like year, month, day, etc.

Example:
import datetime

today = datetime.datetime.now()
print(today)

Output: 2025-09-08 12:34:56.789123

📤 Python Date Output

By default, Python displays dates in the format YYYY-MM-DD HH:MM:SS. You can extract specific parts of the date such as the year, month, or weekday.

Example:
import datetime

x = datetime.datetime.now()

print(x.year)       # 2025
print(x.strftime("%A"))  # Monday

🛠️ Creating Date Objects

You can create a date object using the datetime class. It requires year, month, and day as mandatory arguments.

Syntax:
datetime.datetime(year, month, day)
Example:
import datetime

date1 = datetime.datetime(2025, 9, 8)
print(date1)

Output: 2025-09-08 00:00:00

📝 The strftime() Method

The strftime() method allows you to format date objects into readable strings. This is extremely useful when displaying dates in reports, user interfaces, or logs.

Syntax:
date.strftime(format)
Example:
import datetime

now = datetime.datetime.now()
print(now.strftime("%B %d, %Y"))

Output: September 08, 2025

📖 Reference of Legal Format Codes

Here are the most commonly used format codes with strftime():

CodeDescriptionExample Output
%YYear (full)2025
%yYear (short)25
%mMonth (01–12)09
%BFull month nameSeptember
%bAbbreviated month nameSep
%dDay of month (01–31)08
%AFull weekday nameMonday
%aAbbreviated weekdayMon
%HHour (24-hour)14
%IHour (12-hour)02
%pAM/PMPM
%MMinutes45
%SSeconds09
%fMicroseconds987654
%cLocal date & timeMon Sep 08 14:45:09 2025

🧑‍💻 Exercises

  1. Write a program to display today’s date in “Day, Month Date, Year” format.
  2. Create a date object for your birthday and display the weekday name.
  3. Print the current time in 12-hour format with AM/PM.
  4. Write a program that calculates how many days are left until New Year.

❓ FAQ

Q1: How do I get only the current year?
Use datetime.datetime.now().year.

Q2: How do I compare two dates?
You can subtract one datetime object from another to get a timedelta object.

Q3: What’s the difference between datetime and time modules?
datetime handles both dates and times, while time mostly deals with timestamps and time-related functions.

✅ Conclusion

The Python datetime module is powerful for handling real-world time-based data. You can easily create date objects, format them with strftime(), and use format codes to display output in human-readable ways. By practicing with exercises, you’ll quickly get comfortable with working on date and time projects in Python.


Related Posts

Continue Learning: Explore more beginner-friendly topics and practice materials in our Programming Resource Hub.

Post a Comment

0 Comments