Python datetime.now()

MamtaWardhani's avatar
Published Jun 7, 2022Updated Jun 19, 2025
Contribute to Docs

The datetime.now() method is a class method from Python’s datetime module that returns a datetime object representing the current local date and time. This method captures the exact moment when it is called, including the year, month, day, hour, minute, second, and microsecond components. It serves as the primary way to obtain real-time temporal information within Python applications.

The datetime.now() method is commonly used in applications that require timestamping, logging activities, scheduling tasks, measuring execution time, and creating time-based records. It is essential for web applications that need to track user activity, financial systems that require transaction timestamps, monitoring systems that log events, and any application where recording the current moment is crucial for functionality or compliance.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

datetime.now(tz=None)

Parameters:

  • tz (Optional): Timezone parameter that specifies the timezone for which the current date and time should be returned. If None (default), returns the current local date and time. Accepts a timezone object that implements the tzinfo abstract base class.

Return value:

A datetime object representing the current date and time. The returned object contains attributes for year, month, day, hour, minute, second, and microsecond.

Example 1: Getting Current Date and Time

This example demonstrates the basic usage of datetime.now() to retrieve the current date and time:

# Import the datetime class from datetime module
from datetime import datetime
# Get the current date and time
current_datetime = datetime.now()
# Display the complete datetime object
print("Current date and time:", current_datetime)
# Access individual components
print("Year:", current_datetime.year)
print("Month:", current_datetime.month)
print("Day:", current_datetime.day)
print("Hour:", current_datetime.hour)
print("Minute:", current_datetime.minute)
print("Second:", current_datetime.second)

The output of the code is:

Current date and time: 2025-06-08 14:30:45.123456
Year: 2025
Month: 6
Day: 8
Hour: 14
Minute: 30
Second: 45

Note: The actual output will change every time the code is run, because datetime.now() captures the exact current date and time at the moment of execution.

This example shows how to import the datetime class and use the now() method to get the current timestamp. The output displays both the complete datetime object and its individual components, making it easy to extract specific time elements when needed.

Example 2: Event Logging System

This example demonstrates using datetime.now() to create an event logging system that timestamps important application events:

from datetime import datetime
class EventLogger:
def __init__(self):
self.events = []
def log_event(self, event_type, description):
# Capture the exact moment when event occurs
timestamp = datetime.now()
# Create event record with timestamp
event_record = {
'timestamp': timestamp,
'type': event_type,
'description': description,
'formatted_time': timestamp.strftime("%Y-%m-%d %H:%M:%S")
}
# Store the event
self.events.append(event_record)
print(f"[{event_record['formatted_time']}] {event_type}: {description}")
def get_events_summary(self):
print(f"\nTotal events logged: {len(self.events)}")
for event in self.events:
print(f"- {event['formatted_time']}: {event['type']}")
# Create logger and log various events
logger = EventLogger()
logger.log_event("USER_LOGIN", "User johndoe logged into the system")
logger.log_event("FILE_UPLOAD", "Document report.pdf uploaded successfully")
logger.log_event("ERROR", "Database connection timeout occurred")
# Display summary
logger.get_events_summary()

The output of this code will be:

[2025-06-08 14:30:45] USER_LOGIN: User johndoe logged into the system
[2025-06-08 14:30:46] FILE_UPLOAD: Document report.pdf uploaded successfully
[2025-06-08 14:30:47] ERROR: Database connection timeout occurred
Total events logged: 3
- 2025-06-08 14:30:45: USER_LOGIN
- 2025-06-08 14:30:46: FILE_UPLOAD
- 2025-06-08 14:30:47: ERROR

This example shows a practical application where datetime.now() is used to timestamp events in a logging system. Each event is recorded with the precise moment it occurred, which is essential for debugging, auditing, and monitoring system activity.

Codebyte Example: Task Scheduler with Deadlines

This example demonstrates using datetime.now() in a task management system to track deadlines and calculate remaining time:

Code
Output

This example demonstrates a task scheduler that uses datetime.now() to track when tasks are created and calculate remaining time until deadlines. This is useful for project management applications, reminder systems, and time-sensitive workflow management.

Frequently Asked Questions

1. Does datetime.now() return the same time zone every time?

Yes, datetime.now() without parameters always returns the current local time based on the system’s timezone settings. To get time in a specific timezone, pass a timezone object as the tz parameter.

2. Can I use datetime.now() to get UTC time?

While datetime.utcnow() is still available, it returns a naive UTC time. For timezone-aware applications, use datetime.now(timezone.utc) instead.

3. What happens if the system clock changes while my program is running?

datetime.now() reflects the current system time, so if the system clock is adjusted, subsequent calls will return the new time. This can affect time-based calculations in long-running applications.

4. Is datetime.now() thread-safe?

Yes, datetime.now() is thread-safe and can be called from multiple threads simultaneously without causing race conditions or data corruption.

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours