How to get current date and time in Python?
1. Using datetime.now() (Most Common Method)
Retrieves the current local date and time with full precision.
from datetime import datetime
current_datetime = datetime.now()
print(current_datetime)Returns date and time including microseconds.
2. Get Only Current Date
from datetime import date
today = date.today()
print(today)Useful for applications requiring date-only tracking.
3. Get Only Current Time
from datetime import datetime
current_time = datetime.now().time()
print(current_time)Extracts only the time component.
4. Formatted Current Date and Time
Ideal for logs, UI display, and reports.
5. Using time Module
Returns a human-readable timestamp string.
6. Using UNIX Timestamp
Returns seconds since Jan 1, 1970 (Epoch time).
7. Current UTC Date and Time
Used in distributed systems and APIs.
8. Current Date and Time with Timezone
Provides timezone-aware timestamps.
9. Human-Friendly Display
Used for dashboards and user notifications.
10. Enterprise Logging Example
Standard pattern for system and audit logs.
Summary Table
datetime.now()
datetime object
Full timestamp
date.today()
date object
Only date
datetime.now().time()
time object
Only time
time.time()
float
Unix timestamp
time.ctime()
string
Readable timestamp
datetime.utcnow()
datetime
UTC date/time
Best Practices
Use
datetime.now()for local applicationsUse
datetime.utcnow()or timezone-aware datetime for APIsFormat output with
strftime()for displayStore time internally as UTC in distributed systems
Last updated