What Are The Dates?
Dates represent real-world calendar values like day, month, year, time, timezone, etc. Python doesn’t automatically understand dates as numbers or strings, so it provides a special module “datetime” to add dates and times in our program.
datetime module allows us to can:
- Create today’s date
- Work with past or future dates
- Format dates into readable strings
- Extract details like year, month, day, hour
- Calculate differences between dates
These topics are essential because we are using dates in multiple important sectors, like:
- Automation: Schedule tasks or log timestamps in applications.
- Data Analysis: Analyze time-based data such as trends or logs.
- User-Friendly Applications: Display and manipulate dates in applications.
Let’s Learn About Python Dates
First, we need to import the datetime module because it provides the following classes:
- date
- time
- datetime
- timedelta
How To Import a datetime Module
import datetime
Simple Example of Data and Time
from datetime import date
# Get today's date
today = date.today()
# Extract details
current_year = today.year
current_month = today.month
current_day = today.day
print("Today's Complete Date:", today)
print("Only Year:", current_year)
print("Only Month:", current_month)
print("Only Day:", current_day)
Output Look Like:
Today's Complete Date: 2025-12-12
Only Year: 2025
Only Month: 12
Only Day: 12
How Does the time Class Work in Python?
The Python time class is part of the datetime module that is used when you want to work with time only, without involving any date.
This function is helpful when we need:
- The timing of an event (example: app login time)
- To display time in a user interface
- To log activity (like “task completed at 16:45:10”)
- To store or compare times (like “alarm set for 07:00:00”)
Let’s Create a Time Object
Example: Creating a time object for a shop opening time
import datetime
opening_time = datetime.time(9, 15, 30) # 09:15:30 AM
print("Shop Opens At:", opening_time)
Output:
Shop Opens At: 09:15:30
- Here, we create a time object that represents 9:15 AM and 30 seconds.
How To Access a Time Components
Example: Student waking-up routine time
import datetime
wake_time = datetime.time(6, 45, 5)
print("Wake-up Hour:", wake_time.hour)
print("Wake-up Minute:", wake_time.minute)
print("Wake-up Second:", wake_time.second)
Output:
Wake-up Hour: 6
Wake-up Minute: 45
Wake-up Second: 5
Combining Date and Time
You will use data and time separately in real-world projects, but sometimes we need both together. For example:
- Storing when a meeting starts
- Recording when a user logs in
- Saving the exact timestamp of a transaction
Example:
import datetime
# Creating a date and time together (Year, Month, Day, Hour, Minute, Second)
meeting_time = datetime.datetime(2025, 3, 18, 9, 15, 20)
print("Your Meeting Starts At:", meeting_time)
Output:
Your Meeting Starts At: 2025-03-18 09:15:20
Formatting Dates and Times
Formatting dates and times simply means converting a datetime object into a human-readable string.
Python stores dates and times internally in its own structured format, but humans prefer formats like:
- 12-11-2024
- Wednesday, 11 December 2024
- 14:30:45
- 11 Dec, 2024 – 02:30 PM
Python provides the strtime() method that allows you to design the exact layout you want by using format codes.
Example: Formatting Date and Time
import datetime
# Get the current date and time
current_dt = datetime.datetime.now()
# Create different readable formats
simple_date = current_dt.strftime("%d-%m-%Y") # Day-Month-Year
friendly_date = current_dt.strftime("%A, %B %d") # Weekday, Month Day
twelve_hour_time = current_dt.strftime("%I:%M %p") # 12-hour format
twenty_four_time = current_dt.strftime("%H:%M:%S") # 24-hour format
print("Simple Date Format:", simple_date)
print("Friendly Date Format:", friendly_date)
print("12-Hour Time Format:", twelve_hour_time)
print("24-Hour Time Format:", twenty_four_time)
Output:
Simple Date Format: 11-12-2024
Friendly Date Format: Wednesday, December 11
12-Hour Time Format: 02:30 PM
24-Hour Time Format: 14:30:45
Common Format Codes
- %Y: Year (e.g., 2024)
- %m: Month (e.g., 12)
- %d: Day (e.g., 11)
- %H: Hour (24-hour format)
- %M: Minute
- %S: Second
Date Arithmetic with timedelta
Python provides a time delta class that we can use for adding, subtracting, or comparing dates in a very simple way.
The timedelta is a tool that represents a time gap, like “10 days”, “3 hours”, or “2 weeks”. You don’t have to manually calculate dates; Python does everything for you.
Example 1: Adding Days to a Date
import datetime
today = datetime.date.today()
# Adding 10 days to the current date
future_date = today + datetime.timedelta(days=10)
print("Today's Date:", today)
print("Date After 10 Days:", future_date)
Output:
Today's Date: 2025-12-11
Date After 10 Days: 2025-12-21
- datetime.date.today() → gives today’s date
- datetime.timedelta(days=10) → represents a “10-day gap”
Example 2: Calculating Difference Between Two Dates
import datetime
start_date = datetime.date(2024, 12, 11)
end_date = datetime.date(2024, 12, 25)
gap = end_date - start_date # Returns a timedelta object
print("Start Date:", start_date)
print("End Date:", end_date)
print("Number of Days Between:", gap.days)
Output:
Start Date: 2024-12-11
End Date: 2024-12-25
Number of Days Between: 14
Working with Time Zones in Python
Time zones refer to the location where the time is based (e.g., India, US, UK) and adjust hours accordingly.
Example: Converting UTC Time to IST
from datetime import datetime
import pytz
# Current UTC time
current_utc = datetime.now(pytz.utc)
# Convert UTC → IST
ist_zone = pytz.timezone("Asia/Kolkata")
current_ist = current_utc.astimezone(ist_zone)
print("UTC Time :", current_utc.strftime("%Y-%m-%d %H:%M:%S"))
print("Indian Time :", current_ist.strftime("%Y-%m-%d %H:%M:%S"))
Output of the code:
UTC Time : 2024-12-11 10:15:30
Indian Time : 2024-12-11 15:45:30
- What Are Modules In Python?
- What Is a Function In Python?
- What Is Python String Formatting?
- What Is a Python List?
- How Can We Use a Lambda Function?

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.