Last modified: May 04, 2026 By Alexander Williams
Python datetime now: Get Current Date & Time
Working with dates and times is a common task in Python. The datetime module provides powerful tools for this. One of the most used functions is datetime.now(). It returns the current local date and time.
This article will teach you how to use datetime.now() effectively. You will learn about timezones, formatting, and common use cases. We will use clear examples and code snippets.
Let's start with the basics. The datetime module is part of Python's standard library. You don't need to install anything extra. Just import it and you are ready.
What is datetime.now()?
The datetime.now() method returns a datetime object. This object contains the current year, month, day, hour, minute, second, and microsecond. It uses your system's local time.
Here is a simple example:
import datetime
# Get current date and time
now = datetime.datetime.now()
print("Current date and time:", now)
Current date and time: 2025-04-02 14:30:45.123456
The output shows the current moment. The format is YYYY-MM-DD HH:MM:SS.mmmmmm. This is the standard ISO 8601 format.
Getting Only Date or Time
Sometimes you need just the date or just the time. The datetime object has methods for that. Use .date() to get the date part. Use .time() to get the time part.
import datetime
now = datetime.datetime.now()
# Get only the date
current_date = now.date()
print("Current date:", current_date)
# Get only the time
current_time = now.time()
print("Current time:", current_time)
Current date: 2025-04-02
Current time: 14:30:45.123456
This is useful when you need to compare dates or times separately. For example, checking if a date is in the past or future.
Working with Timezones
The datetime.now() function returns local time. But many applications need UTC (Coordinated Universal Time). UTC is timezone-independent and avoids confusion.
To get UTC time, use datetime.now(timezone.utc). You need to import timezone from the datetime module.
import datetime
from datetime import timezone
# Get current UTC time
utc_now = datetime.datetime.now(timezone.utc)
print("UTC time:", utc_now)
# Get current local time
local_now = datetime.datetime.now()
print("Local time:", local_now)
UTC time: 2025-04-02 12:30:45.123456+00:00
Local time: 2025-04-02 14:30:45.123456
Notice the UTC time includes the +00:00 offset. This indicates it is UTC. The local time has no offset because it is naive (no timezone info).
For more advanced timezone handling, check out our Master Python Datetime Guide.
Formatting the Output
Raw datetime objects are not always user-friendly. You can format them using the strftime() method. This method converts a datetime object to a string with a specific format.
Common format codes include:
- %Y - Year with century (e.g., 2025)
- %m - Month as zero-padded number (01-12)
- %d - Day of month (01-31)
- %H - Hour (00-23)
- %M - Minute (00-59)
- %S - Second (00-59)
import datetime
now = datetime.datetime.now()
# Format as "April 02, 2025"
formatted1 = now.strftime("%B %d, %Y")
print("Formatted date:", formatted1)
# Format as "14:30:45"
formatted2 = now.strftime("%H:%M:%S")
print("Formatted time:", formatted2)
# Format as "2025-04-02 14:30"
formatted3 = now.strftime("%Y-%m-%d %H:%M")
print("Formatted datetime:", formatted3)
Formatted date: April 02, 2025
Formatted time: 14:30:45
Formatted datetime: 2025-04-02 14:30
You can create any format you need. This is perfect for reports, logs, or user interfaces.
Using datetime.now() in Real Applications
Let's see a practical example. Imagine you are building a logging system. You want to record when an event happens.
import datetime
def log_event(event_name):
# Get current timestamp
timestamp = datetime.datetime.now()
# Format it nicely
formatted_time = timestamp.strftime("%Y-%m-%d %H:%M:%S")
# Print the log
print(f"[{formatted_time}] Event: {event_name}")
# Test the function
log_event("User logged in")
log_event("Data saved successfully")
[2025-04-02 14:30:45] Event: User logged in
[2025-04-02 14:30:46] Event: Data saved successfully
This is a simple but useful pattern. You can extend it to write logs to a file or database.
Another common use is measuring how long something takes. You can get the time before and after an operation.
import datetime
# Record start time
start = datetime.datetime.now()
# Simulate a task (e.g., a loop)
total = sum(range(1000000))
# Record end time
end = datetime.datetime.now()
# Calculate duration
duration = end - start
print(f"Task took {duration.total_seconds():.2f} seconds")
Task took 0.04 seconds
The result is a timedelta object. You can convert it to seconds, minutes, or hours.
Common Mistakes to Avoid
Beginners often forget to import the module. Always start with import datetime. Another mistake is using datetime.now() without considering timezones. If your app runs globally, use UTC.
Also, do not confuse datetime.now() with datetime.today(). Both return the current local time. But datetime.today() does not support timezone argument. Use datetime.now() for consistency.
For a deeper dive into all datetime features, refer to our Master Python Datetime Guide.
Conclusion
The datetime.now() function is essential for any Python developer. It gives you the current date and time with ease. You can get local time, UTC time, or format it as needed. Use it for logging, timestamps, and performance measurements.
Remember to import the module correctly. Use timezone-aware objects for global applications. Practice with the examples in this article. You will soon master datetime handling.
Start using datetime.now() in your projects today. It is simple, powerful, and reliable.