🕯️ Magic Note
The time module must be imported before use. time.strftime() without passing a time object uses the current local time. The format codes are the magic symbols. %H is the hour in 24 hour format (00 to 23). %M is the minute (00 to 59). %S is the second (00 to 61, accounting for leap seconds).
- Requires import time at the top of your script
- %H gives 24 hour format (00 to 23)
- %I (capital I) gives 12 hour format (01 to 12)
- %p gives AM or PM for 12 hour clock
- %M gives minutes, %S gives seconds
| Format Code | Meaning | Example Output |
|---|---|---|
| %H | Hour (24 hour, 00-23) | 14 |
| %I | Hour (12 hour, 01-12) | 02 |
| %M | Minute (00-59) | 35 |
| %S | Second (00-61) | 22 |
| %p | AM or PM | PM |
| %H:%M:%S | Full 24 hour time | 14:35:22 |
Python
# Basic current time
import time
now = time.strftime(“%H:%M:%S”)
print(now)
# Output: 14:35:22 (current time will vary)
Python
# 12 hour clock with AM/PM
import time
now = time.strftime(“%I:%M:%S %p”)
print(now)
# Output: 02:35:22 PM (current time will vary)
Python
# Full date and time
import time
now = time.strftime(“%Y-%m-%d %H:%M:%S”)
print(now)
# Output: 2025-04-21 14:35:22 (current date and time will vary)
- Forgetting to import the time module before using strftime()
- Confusing %m (month) with %M (minute), they are case sensitive
- Using %I (capital i) for hour but typing %l (lowercase L) which is not a valid code
⚡ Whisper
This second will never come again. The clock ticks forward. Silence between the beats. Python captures the now, prints it quietly, and lets it fade. Stay here. Just this moment. Just this gift.