VMware ESXi and vSphere Cluster Management

Get the Current Date and Time in Python

Learn how to use Python's datetime module to retrieve and display the current local date, time, and combined date-time value.

Python's standard library includes the datetime module for working with calendar dates, clock times, and combined date-time values. This lesson shows how to retrieve the current local date and time with datetime.datetime.now().

Import the datetime module

Use an import statement before calling functionality from the module:

import datetime

This statement makes the module available under the name datetime. The module contains a class that is also named datetime, so the complete name of that class is datetime.datetime.

Get the current local date and time

Call the now() class method on datetime.datetime:

import datetime

current_date_time = datetime.datetime.now()
print(current_date_time)

datetime.datetime.now() returns a datetime object. It contains both a calendar date and a clock time:

  • Date components: year, month, and day
  • Time components: hour, minute, and second
  • Fractional seconds: microseconds when available

The printed result commonly resembles 2026-08-18 14:37:52.123456. The actual value depends on the machine's clock and the moment when the program runs. Example timestamps are illustrative, not fixed output.

The value returned by now() uses the local time configured on the computer running the program. It does not automatically represent a different city, time zone, or UTC.

Get only the current date

Call date() on the datetime object to keep only its calendar-date portion:

import datetime

current_date = datetime.datetime.now().date()
print(current_date)

The result is a date object, not a full datetime object. It contains the year, month, and day, and it is typically displayed in an ISO-like format such as 2026-08-18.

Get only the current time

Call time() on the datetime object to keep only its clock-time portion:

import datetime

current_time = datetime.datetime.now().time()
print(current_time)

The result is a time object. It contains the hour, minute, and second, and it may also contain microseconds. A typical display could be 14:37:52.123456.

Compare the returned object types

ExpressionReturned object typeContainsTypical use
datetime.datetime.now()datetimeDate and time, potentially microsecondsTimestamping and combined date-time display
datetime.datetime.now().date()dateYear, month, and dayDate-only displays and date comparisons
datetime.datetime.now().time()timeHour, minute, second, and potentially microsecondsTime-only displays and time-of-day use cases

A datetime object combines the information in a date and a time. A date object has no clock-time component, while a time object has no calendar-date component.

Read individual components

Store one datetime value in a variable, then access its attributes with dot notation:

import datetime

current = datetime.datetime.now()

print(current.year)
print(current.month)
print(current.day)
print(current.hour)
print(current.minute)
print(current.second)

These attributes return numeric values. For example, month is a number from 1 through 12, and day is the day of the month. The microsecond attribute provides the fractional-second component when you need it:

print(current.microsecond)

Why store the result of now()

Every call to datetime.datetime.now() retrieves the time at that particular moment. If you call it separately for a date and a time, the program could cross a second, minute, or even day boundary between calls.

When multiple values must describe exactly the same instant, call now() once and reuse the object:

import datetime

current = datetime.datetime.now()
current_date = current.date()
current_time = current.time()

print(current_date)
print(current_time)

Common problem: calling datetime.now() after import datetime

This code is a common source of confusion:

import datetime

# Incorrect with this import style:
# current = datetime.now()

After import datetime, the name datetime refers to the module. The now() method belongs to the module's datetime class, so use:

current = datetime.datetime.now()

An alternative import style imports the class directly:

from datetime import datetime

current = datetime.now()

Both styles work. They differ only in which name refers to the class.

Troubleshooting local time and microseconds

  • Unexpected location's time: now() follows the host machine's local time settings. Check the computer's configured time zone when the result is unexpected. For a specific region or UTC, use timezone-aware datetime techniques such as zoneinfo.
  • Unexpected digits after seconds: Microseconds are a normal part of datetime and time values. Use formatting when a shorter display is required.
  • Different results on different runs: The clock advances, so output changes each time the script runs.
  • Different date and time values from separate calls: Store one result from now() and derive both values from that object.

Exam-relevant summary

  • datetime is a Python standard-library module for date and time values.
  • import datetime imports the module; it does not directly import the class.
  • datetime.datetime.now() returns the current local date and time as a datetime object.
  • datetime.datetime.now().date() returns a date object containing year, month, and day.
  • datetime.datetime.now().time() returns a time object containing clock-time fields and possibly microseconds.
  • The displayed value depends on the machine's local clock and the instant of execution.

After learning these basics, continue with date and time work in Python, including formatting, parsing, time zones, and date arithmetic.