1. Using datetime.now().time() (Standard Method)
Retrieves the current local time as a time object.
from datetime import datetime
current_time = datetime.now().time()
print(current_time)
Includes hours, minutes, seconds, and microseconds.
2. Using strftime() for Formatted Time
from datetime import datetime
now = datetime.now()
formatted_time = now.strftime("%H:%M:%S")
print(formatted_time)
Ideal for logs and UI display.
3. 12-Hour Format with AM/PM
from datetime import datetime
time_12_hour = datetime.now().strftime("%I:%M %p")
print(time_12_hour)
Common in user-facing applications.
4. Using the time Module (System Clock)
Reads the system clock directly.
5. Get Only Hour, Minute, Second
Useful for conditional logic and scheduling.
6. Get Current UTC Time
Recommended for distributed and API-based systems.
7. Timezone-Aware Current Time
Ensures accurate time across regions.
8. Current Time with Microseconds
Used in high-precision logging.
9. UNIX Time (Epoch Seconds)
Represents time in seconds since Jan 1, 1970.
10. Enterprise Logging Example (Time-Only Logger)
Standard pattern for time-stamped events.
Method
Output
Best Use Case
Use timezone-aware times for global systems
Prefer UTC for backend storage
Format time only at presentation layer
Avoid relying solely on system locale defaults
Last updated