Practical guide · Python

Getting Current Date and Time in Python: Modern Approaches Every Developer Should Know

Choose the right Python time API for local dates, readable timestamps, UTC records, Unix time, logging, and measuring elapsed work.

Current date and time values appear in logs, audit records, reports, scheduled jobs, and data pipelines. Python's standard library offers several ways to get them, but the right choice depends on whether you need local wall-clock time, a globally comparable timestamp, or an elapsed duration. This guide covers the common approaches and the timezone details that keep timestamps useful.

1. Get the current local date and time

Use datetime.now() for the current local date and time. It returns a datetime object, which you can compare, format, or pass to other code without immediately converting it to text.

from datetime import datetime

current_time = datetime.now()
print(current_time)
# Example: 2026-11-04 17:50:15.123456

The returned value uses the machine's local clock and is timezone-naive: it does not record which timezone that clock represents. Use it for local scripts or display-oriented tasks when the machine's configured timezone is the intended one.

2. Get only the date or time

Use date.today() when you need just the local calendar date:

from datetime import date

today = date.today()
print(today)
# Example: 2026-11-04

To get just the local time of day, take the time portion of the current local datetime:

from datetime import datetime

current_time = datetime.now().time()
print(current_time)
# Example: 17:50:15.123456

This time value is also timezone-naive. If timezone information matters, keep a timezone-aware datetime instead of discarding its date and timezone context.

3. Format a date or time for display

Keep values as date or datetime objects while your program is working with them. Use strftime() when you need a specific text representation:

from datetime import datetime

current_time = datetime.now()
formatted_time = current_time.strftime("%H:%M:%S")
print(formatted_time)
# Example: 17:50:15

Common format codes include:

For example, datetime.now().strftime("%d-%m-%Y %I:%M:%S %p") produces a value like 04-11-2026 05:50:15 PM. Choose a format that is unambiguous for your audience; for machine-readable timestamps, ISO 8601 is often a better fit.

4. Create timezone-aware UTC timestamps

For services, APIs, distributed applications, and records shared across regions, use a timezone-aware UTC datetime:

from datetime import datetime, timezone

current_utc = datetime.now(timezone.utc)
print(current_utc)
# Example: 2026-11-04 12:20:15.123456+00:00

The UTC offset is part of the value, so it is clear which instant the timestamp represents. Prefer this over datetime.utcnow(), which returns a timezone-naive value and is deprecated starting in Python 3.12. Convert to a user's local timezone when displaying the value rather than changing the stored instant.

For an ISO 8601 string suitable for an API payload, use isoformat():

api_timestamp = current_utc.isoformat()
print(api_timestamp)
# Example: 2026-11-04T12:20:15.123456+00:00

5. Get a Unix timestamp

A Unix timestamp represents seconds since the Unix epoch. Use time.time() when you need the current system timestamp as a number, or get it from an aware datetime:

import time
from datetime import datetime, timezone

timestamp_now = time.time()
timestamp_from_datetime = datetime.now(timezone.utc).timestamp()
print(timestamp_now)
print(timestamp_from_datetime)

These values are floating-point seconds, so their printed precision depends on the system and formatting. A timestamp is useful for interchange and storage, but it does not preserve the original timezone or a human-friendly calendar representation on its own.

6. Use time correctly in common tasks

Logging events

Use Python's logging module instead of printing ad hoc timestamp strings throughout an application. Include an explicit UTC timestamp in the message when a consistent timezone is important:

import logging
from datetime import datetime, timezone

logging.basicConfig(level=logging.INFO)
logging.info("Application started at %s", datetime.now(timezone.utc).isoformat())

Measuring elapsed time

For duration or performance measurements, use time.perf_counter(). It is a high-resolution monotonic clock, so wall-clock adjustments do not make an elapsed interval jump backwards or forwards:

import time

start = time.perf_counter()
# Run the operation to measure.
elapsed_seconds = time.perf_counter() - start
print(f"Execution time: {elapsed_seconds:.6f} seconds")

Use time.time() for a wall-clock timestamp, not as the preferred clock for benchmarking.

Adding a creation time to a record

For records that may be read across machines or regions, store an aware UTC datetime and use a database column type and convention that preserve that meaning:

from datetime import datetime, timezone

user = {
    "name": "Suresh",
    "created_on": datetime.now(timezone.utc),
}

Common mistakes and best practices

Quick reference: use datetime.now() for local date and time, datetime.now(timezone.utc) for an aware UTC datetime, time.time() for a Unix timestamp, and time.perf_counter() for elapsed duration.