Python time Module – Complete Guide With Examples
The time module in Python is used to work with date & time values, timestamps, delays, stopwatch features, and time formatting. It is one of the most important built-in modules when you want to:
- Display the current date and time
- Measure execution time of programs
- Convert timestamps into readable formats
- Pause program execution (sleep)
- Calculate time differences
Basic Example of the Time Module
import time
print(time.time()) # returns current timestamp (seconds since 1970)
print(time.localtime(time.time())) # converts timestamp to local time structure
print(time.asctime(time.localtime(time.time()))) # converts to readable date-time format
Output Example:
1735942230.7655
time.struct_time(tm_year=2025, tm_mon=1, tm_mday=15, tm_hour=11, tm_min=10, tm_sec=30, ...)
Wed Jan 15 11:10:30 2025
Breakdown of time.struct_time Values
When you call time.localtime(), Python returns a structure containing:
- tm_year – Year
- tm_mon – Month
- tm_mday – Day of month
- tm_hour – Hours
- tm_min – Minutes
- tm_sec – Seconds
- tm_wday – Day of week
- tm_yday – Day of year
Example: Display Custom Date Format
import time
d = time.localtime(time.time())
print(str(d.tm_year) + ":" + str(d.tm_mon) + ":" + str(d.tm_mday))
Useful Functions of the Time Module
1. time.sleep() – Pause Program Execution
import time
print("Start")
time.sleep(2) # waits 2 seconds
print("End")
2. Measure Execution Time (Stopwatch)
import time
start = time.time()
# some code
for i in range(1000000):
pass
end = time.time()
print("Execution time:", end - start, "seconds")
3. Get Current Time in Different Formats
import time
print(time.strftime("%Y-%m-%d"))
print(time.strftime("%H:%M:%S"))
print(time.strftime("%d-%m-%Y %I:%M:%S %p"))
Output Example:
2025-01-15
11:18:25
15-01-2025 11:18:25 AM
Formatting Codes for strftime()
| Format Code | Description |
|---|---|
| %Y | 4-digit year |
| %y | 2-digit year |
| %m | Month (01–12) |
| %d | Day (01–31) |
| %H | Hour (24-hour) |
| %I | Hour (12-hour) |
| %M | Minutes |
| %S | Seconds |
| %p | AM/PM |
| %A | Full weekday name |
| %B | Full month name |
Example: Display Full Date and Time
import time
print(time.strftime("%A, %d %B %Y - %I:%M:%S %p"))
Output:
Wednesday, 15 January 2025 - 11:25:39 AM
Example: Countdown Timer
import time
for i in range(5, 0, -1):
print(i)
time.sleep(1)
print("Time Up!")
Assignment – Time Module Programs
- WAP to create a digital clock that updates every second.
- WAP to measure execution time of bubble sort using the time module.
- WAP to print today’s date in format: DD/MM/YYYY.
- WAP to create a stopwatch with Start/Stop features.
- WAP to calculate difference between two timestamps.
3 Comments
import time
ReplyDeleteprint(time.time)
print(time.localtime(time.time()))
print(time.asctime(time.localtime(time.time())))
import time
ReplyDeletesecond = time.time()
print("second since epoch", second)
import time
ReplyDeletesecond = time.time()
print("second since epoch", second)
POST Answer of Questions and ASK to Doubt