Get the Current Date and Time in Python
Learn how to use Python's datetime module to retrieve, print, format, and work with the current local date and time.
Python's standard-library datetime module provides classes and methods for representing and working with dates and times. You can use it to retrieve the current local date, the current local time, or both.
This lesson assumes that you know how to run a Python script, assign values to variables, call methods, use import, and print values with print().
Import the datetime Module
There are two common ways to import the datetime functionality. The first imports the complete module:
import datetime
With this form, datetime refers to the module. The module contains a class also named datetime. Therefore, you use the module-qualified name datetime.datetime to refer to that class.
import datetime
current_date_time = datetime.datetime.now()
print(current_date_time)
The second form imports the datetime class directly:
from datetime import datetime
current_date_time = datetime.now()
print(current_date_time)
Both examples use the same class. The import style changes the name you write in the rest of the program: datetime.datetime.now() with the module import, or datetime.now() with the direct class import.
Get the Current Date and Time
The now() class method creates a datetime object representing the current local date and time when no timezone is supplied.
import datetime
current_date_time = datetime.datetime.now()
print(current_date_time)
A typical result looks like this:
2026-08-18 14:56:08.800197
The exact value depends on when and where the program runs. A datetime value contains the year, month, day, hour, minute, second, and usually microseconds. Microseconds are millionths of a second.
Get Only the Current Date
Call date() on the datetime object to extract its calendar date:
import datetime
current_date = datetime.datetime.now().date()
print(current_date)
Typical output is:
2026-08-18
A date object contains a year, month, and day, but no clock time. Date objects are useful for date-only comparisons, such as checking whether a due date has passed, and for records that do not need a time of day.
Get Only the Current Time
Call time() on the datetime object to extract its clock time:
import datetime
current_time = datetime.datetime.now().time()
print(current_time)
Typical output is:
14:57:47.801347
A time object contains hours, minutes, seconds, and potentially microseconds. It does not contain a calendar date.
Methods for Retrieving the Current Date and Time
Understand the Default Output
When printed without custom formatting, these values generally use the following layouts:
- Datetime:
YYYY-MM-DD HH:MM:SS.microseconds - Date:
YYYY-MM-DD - Time:
HH:MM:SS.microseconds
The year-month-day order follows an unambiguous ISO-style layout. Microseconds may appear when their value is nonzero. The actual date and time always vary according to the execution time and the computer or runtime environment.
Local Time and Timezone Awareness
datetime.datetime.now() without an argument uses the local system clock and returns a naive datetime. A naive datetime has no timezone information attached to it. It represents a clock reading, but the object itself does not say which timezone that reading belongs to.
A timezone-aware datetime is associated with a timezone or UTC offset. When a timezone-independent timestamp is needed, request the current time in UTC:
import datetime
current_utc = datetime.datetime.now(datetime.timezone.utc)
print(current_utc)
The output includes a +00:00 offset, indicating UTC:
2026-08-18 12:56:08.800197+00:00
You can also import the class and timezone directly:
from datetime import datetime, timezone
current_utc = datetime.now(timezone.utc)
print(current_utc)
Local machine settings affect the result of now(). The operating system's timezone configuration, system clock, container settings, or hosting environment may cause the displayed local time to differ from your expectation. Use an explicit timezone such as UTC when systems need to exchange a consistent timestamp.
Format Date and Time for Display
The default representation is useful for inspection, but user-facing output often needs a specific format. The strftime() method converts a date, time, or datetime object into a string using format directives.
import datetime
current_date_time = datetime.datetime.now()
print(current_date_time.strftime("%Y-%m-%d %H:%M:%S"))
Example output:
2026-08-18 14:57:47
You can format a date and a clock time separately:
import datetime
current_date_time = datetime.datetime.now()
current_date = current_date_time.date()
current_time = current_date_time.time()
print(current_date.strftime("%Y-%m-%d"))
print(current_time.strftime("%H:%M:%S"))
For example, a display-friendly format could use %Y-%m-%d for a date and %H:%M:%S for a clock time. strftime() returns a string. In contrast, now() returns a datetime object, while date() and time() return date and time objects.
Choose the Right Import Style
These two complete examples are equivalent:
import datetime
stamp = datetime.datetime.now()
print(stamp)
from datetime import datetime
stamp = datetime.now()
print(stamp)
Use the module-qualified form when you want the origin of the class to be especially clear or when using several names from the module. Use the direct-import form when the shorter syntax improves readability. If you use the direct form, remember that datetime now names the class in your code, not the module.
Troubleshooting
datetime.now() Raises an AttributeError
If your code begins with import datetime, the name datetime is the module. Change the call to datetime.datetime.now(), or change the import to from datetime import datetime and then call datetime.now().
The Displayed Time Is in the Wrong Timezone
datetime.datetime.now() follows the environment's configured local timezone and system clock. Check the operating system or runtime timezone settings. For a consistent reference, use datetime.datetime.now(datetime.timezone.utc).
Microseconds Appear in the Output
Datetime objects track microseconds, and their default string representation can display them. Omit them for display with:
print(current_date_time.strftime("%Y-%m-%d %H:%M:%S"))
A Date or Time Does Not Combine with Text as Expected
The values returned by now(), date(), and time() are objects, not preformatted display strings. Use strftime() to select a format, or explicitly convert a value with str() when that is appropriate.
Quick Reference
- Import the module with
import datetime. - Call
datetime.datetime.now()for the current local date and time. - Call
datetime.datetime.now().date()for only the date. - Call
datetime.datetime.now().time()for only the time. - Use
datetime.datetime.now(datetime.timezone.utc)for an aware UTC datetime. - Use
strftime()to turn a date or time object into a formatted string.
For related Python foundations, see Import Modules, What Are Modules, and Strings.