Python Date and Time
Introduction
Many real-world programs work with:
- Current date
- Current time
- User birthdays
- Scheduling systems
- Log files
- Event management
- Timestamps
- Reports
- Time calculations
Python provides the built-in:
datetime
module to work with dates and times.
Why Date and Time Are Important
Examples:
Login Time
Order Date
File Creation Date
Exam Schedule
Employee Attendance
Without date and time handling, many applications would not function correctly.
The datetime Module
Import it:
import datetime
Or:
from datetime import datetime
Date and Time Classes
The most commonly used classes are:
| Class | Purpose |
|---|---|
| date | Stores only date |
| time | Stores only time |
| datetime | Stores date and time |
| timedelta | Time difference |
| timezone | Time zones |
Class 1: date
Represents:
Year
Month
Day
Creating a Date
from datetime import date
d = date(2025, 8, 15)
print(d)
Output:
2025-08-15
Accessing Date Components
from datetime import date
d = date(2025, 8, 15)
print(d.year)
print(d.month)
print(d.day)
Output:
2025
8
15
Today's Date
from datetime import date
today = date.today()
print(today)
Example Output:
2025-08-03
Class 2: time
Represents:
Hour
Minute
Second
Microsecond
Creating Time Object
from datetime import time
t = time(14, 30, 45)
print(t)
Output:
14:30:45
Accessing Components
print(t.hour)
print(t.minute)
print(t.second)
Output:
14
30
45
Class 3: datetime
Most commonly used class.
Contains:
Date + Time
Creating datetime Object
from datetime import datetime
dt = datetime(
2025,
8,
15,
14,
30,
45
)
print(dt)
Output:
2025-08-15 14:30:45
Current Date and Time
from datetime import datetime
now = datetime.now()
print(now)
Example:
2025-08-03 14:35:20.123456
Extracting Values
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
Class 4: timedelta
Represents a duration.
Example:
10 Days
5 Hours
30 Minutes
Creating timedelta
from datetime import timedelta
td = timedelta(days=10)
print(td)
Output:
10 days, 0:00:00
Adding Days
from datetime import date
from datetime import timedelta
today = date.today()
future = today + timedelta(days=30)
print(future)
Subtracting Days
past = today - timedelta(days=15)
print(past)
Difference Between Dates
from datetime import date
d1 = date(2025, 8, 15)
d2 = date(2025, 8, 1)
difference = d1 - d2
print(difference)
Output:
14 days, 0:00:00
Different Time Formats
Dates can be represented in many formats.
Example:
Same date:
2025-08-15
15-08-2025
08/15/2025
15 Aug 2025
August 15, 2025
Common Date Formats
| Format | Example |
|---|---|
| YYYY-MM-DD | 2025-08-15 |
| DD-MM-YYYY | 15-08-2025 |
| MM-DD-YYYY | 08-15-2025 |
| DD/MM/YYYY | 15/08/2025 |
| Month DD YYYY | August 15 2025 |
Common Time Formats
24-Hour Format:
14:30:45
12-Hour Format:
02:30:45 PM
Formatting Dates and Times
Python uses:
strftime()
Meaning:
String Format Time
Converts:
Date/Time Object
↓
Formatted String
Basic Example
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d")
print(formatted)
Output:
2025-08-03
Common Format Codes
| Code | Meaning |
|---|---|
| %Y | Full Year |
| %y | Short Year |
| %m | Month |
| %d | Day |
| %H | Hour (24) |
| %I | Hour (12) |
| %M | Minute |
| %S | Second |
| %p | AM/PM |
| %A | Full Weekday |
| %a | Short Weekday |
| %B | Full Month |
| %b | Short Month |
Examples
YYYY-MM-DD
now.strftime("%Y-%m-%d")
Output:
2025-08-03
DD-MM-YYYY
now.strftime("%d-%m-%Y")
Output:
03-08-2025
DD/MM/YYYY
now.strftime("%d/%m/%Y")
Output:
03/08/2025
Month Name
now.strftime("%B")
Output:
August
Weekday Name
now.strftime("%A")
Output:
Sunday
24-Hour Time
now.strftime("%H:%M:%S")
Output:
14:30:45
12-Hour Time
now.strftime("%I:%M:%S %p")
Output:
02:30:45 PM
Converting String to Date
Formatting converts:
Date → String
Parsing converts:
String → Date
Python uses:
strptime()
Meaning:
String Parse Time
Example
from datetime import datetime
date_string = "15-08-2025"
date_object = datetime.strptime(
date_string,
"%d-%m-%Y"
)
print(date_object)
Output:
2025-08-15 00:00:00
Another Example
from datetime import datetime
text = "2025/08/15"
date_obj = datetime.strptime(
text,
"%Y/%m/%d"
)
print(date_obj)
Output:
2025-08-15 00:00:00
Converting Between Formats
Input:
15-08-2025
Convert to:
2025/08/15
Step 1
Parse string:
date_obj = datetime.strptime(
"15-08-2025",
"%d-%m-%Y"
)
Step 2
Format again:
result = date_obj.strftime(
"%Y/%m/%d"
)
Output:
2025/08/15
Full Example
from datetime import datetime
date_string = "15-08-2025"
date_obj = datetime.strptime(
date_string,
"%d-%m-%Y"
)
new_format = date_obj.strftime(
"%Y/%m/%d"
)
print(new_format)
Output:
2025/08/15
Working with Timestamps
Current Timestamp:
from datetime import datetime
print(datetime.now().timestamp())
Output:
1754200000.123456
Convert Timestamp to Date
from datetime import datetime
timestamp = 1754200000
dt = datetime.fromtimestamp(timestamp)
print(dt)
Common Beginner Mistakes
Mistake 1: Forgetting Import
Bad:
now = datetime.now()
Output:
NameError
Good:
from datetime import datetime
Mistake 2: Using Wrong Format Codes
Bad:
datetime.strptime(
"15-08-2025",
"%Y-%m-%d"
)
Output:
ValueError
Correct:
"%d-%m-%Y"
Mistake 3: Mixing Month and Minute
Bad:
%M
thinking:
Month
Actually:
Minute
Month is:
%m
Mistake 4: Using %H With AM/PM
Bad:
"%H:%M %p"
Use:
"%I:%M %p"
for 12-hour format.
Mistake 5: Confusing strftime() and strptime()
Wrong:
strftime()
for parsing.
Remember:
strftime()
Date → String
strptime()
String → Date
Best Practices
✅ Use datetime instead of manually handling dates
✅ Always validate input dates
✅ Use strptime() for user input
✅ Use strftime() for display
✅ Store dates in ISO format
YYYY-MM-DD
✅ Use timedelta for date calculations
Homework
Q1
Display today's date.
Q2
Display current date and time.
Q3
Create:
date(2025, 12, 25)
and print year, month, day.
Q4
Convert:
15-08-2025
into a datetime object.
Q5
Format current date as:
03/08/2025
Q6
Display current time in:
02:30:45 PM
format.
Q7
Add 30 days to today's date.
Q8
Find difference between:
2025-08-15
2025-08-01
Q9
Convert:
2025/08/15
to:
15-Aug-2025
Q10
Convert a timestamp into a datetime object.
Homework Solutions
Solution 1
from datetime import date
print(date.today())
Solution 2
from datetime import datetime
print(datetime.now())
Solution 3
from datetime import date
d = date(2025, 12, 25)
print(d.year)
print(d.month)
print(d.day)
Solution 4
from datetime import datetime
d = datetime.strptime(
"15-08-2025",
"%d-%m-%Y"
)
print(d)
Solution 5
from datetime import datetime
print(
datetime.now().strftime(
"%d/%m/%Y"
)
)
Solution 6
from datetime import datetime
print(
datetime.now().strftime(
"%I:%M:%S %p"
)
)
Solution 7
from datetime import date
from datetime import timedelta
print(
date.today() +
timedelta(days=30)
)
Solution 8
from datetime import date
d1 = date(2025, 8, 15)
d2 = date(2025, 8, 1)
print(d1 - d2)
Solution 9
from datetime import datetime
d = datetime.strptime(
"2025/08/15",
"%Y/%m/%d"
)
print(
d.strftime(
"%d-%b-%Y"
)
)
Solution 10
from datetime import datetime
timestamp = 1754200000
print(
datetime.fromtimestamp(
timestamp
)
)
Quick Revision
| Function/Class | Purpose |
|---|---|
| date | Date only |
| time | Time only |
| datetime | Date and time |
| timedelta | Time difference |
| date.today() | Today's date |
| datetime.now() | Current date and time |
| strftime() | Date → String |
| strptime() | String → Date |
| timestamp() | Date → Timestamp |
| fromtimestamp() | Timestamp → Date |
Summary
Python's datetime module provides powerful tools for handling:
- Dates
- Times
- Timestamps
- Date calculations
- Format conversions
Key concepts:
date
time
datetime
timedelta
strftime()
strptime()
Most real-world applications use:
datetime.now()
strftime()
strptime()
timedelta()
for scheduling, logging, reporting, event tracking, and time-based calculations.