ThisCalculator

Everyday

Get Current Date and Time in Python with datetime.now()

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

Get Current Date and Time in Python with datetime.now()

To get the current date and time in Python, call datetime.now() from the datetime module. This returns a datetime object containing the local date and time, which you can then format or extract components from as needed.

Formula

The core function is datetime.now(tz=None). When called without arguments, it returns the current local date and time based on the system’s time zone. The formula in the calculator above shows the equivalent: datetime.fromtimestamp(t), where t is the number of seconds since January 1, 1970 (Unix epoch). The result is a datetime object with attributes: year, month, day, hour, minute, second, and microsecond.

Worked Example

Suppose the current Unix timestamp is 1700000000 (which corresponds to November 14, 2023, 22:13:20 UTC). Using datetime.fromtimestamp(1700000000) in a time zone-aware environment gives the local time. For example, in UTC+0, it returns 2023-11-14 22:13:20. The calculator above applies this conversion, and the first output shows that exact date and time.

How datetime.now() Works

The datetime module is part of Python’s standard library, so no extra installation is needed. The now() method is a class method that queries the operating system’s clock. It returns a naive datetime object by default, meaning it has no time zone information. If you need time zone-aware objects, pass a tz object (e.g., datetime.now(timezone.utc)).

Internally, now() uses the C library’s gettimeofday() or similar, which provides microsecond precision on most systems. The returned object is immutable, so you cannot modify it directly; instead, you use methods like replace() to create copies.

Formatting the Date and Time

Once you have a datetime object, you often need to display it in a specific format. The strftime() method converts the object to a string using format codes. Common codes include:

  • %Y – four-digit year (e.g., 2025)
  • %m – two-digit month (01-12)
  • %d – two-digit day (01-31)
  • %H – hour in 24-hour format (00-23)
  • %M – minute (00-59)
  • %S – second (00-59)

For example, datetime.now().strftime("%Y-%m-%d %H:%M:%S") yields something like 2025-03-15 14:30:45. This is the most common way to get a readable string.

Time Zones and UTC

By default, datetime.now() returns local time. If you need Coordinated Universal Time (UTC), use datetime.now(timezone.utc). This returns a time zone-aware object. To convert between time zones, use the astimezone() method with a timezone object or a library like pytz (though zoneinfo is built-in from Python 3.9).

A common mistake is assuming now() is UTC. It is not; it is local. For logs or APIs that require UTC, always specify timezone.utc.

Comparing with datetime.utcnow()

Older code often uses datetime.utcnow(), which returns a naive UTC time. However, this method is deprecated since Python 3.12 because it can cause confusion: the returned object has no time zone info, so it’s easy to treat it as local time. The recommended replacement is datetime.now(timezone.utc).

Similarly, datetime.utcfromtimestamp() is deprecated; use datetime.fromtimestamp(t, tz=timezone.utc) instead.

Common Pitfalls and Troubleshooting

One frequent issue is forgetting to import the class: from datetime import datetime. If you import the module (import datetime), you must call datetime.datetime.now().

Another pitfall is mixing naive and aware datetimes. For example, subtracting a naive datetime from an aware one raises a TypeError. Always ensure both operands have the same time zone awareness.

Also, now() returns the current time at the moment of the call. If you call it twice in a row, you might get the same microsecond, but not guaranteed. For high-precision timing, use time.time() or time.perf_counter().

Alternatives and Related Functions

Besides datetime.now(), Python offers other ways to get time:

  • time.time() – returns Unix timestamp as a float.
  • time.localtime() – returns a struct_time in local time.
  • time.gmtime() – returns a struct_time in UTC.
  • date.today() – returns just the current date (no time).

For date arithmetic (e.g., adding days), use timedelta. For parsing strings into datetimes, use datetime.strptime().

Frequently Asked Questions

Q: How do I get only the current time?
A: Use datetime.now().time() to get a time object with hour, minute, second, and microsecond. Or format with strftime("%H:%M:%S").

Q: How do I get the current date in a specific format?
A: Use datetime.now().strftime("%d/%m/%Y") for day/month/year. Adjust the format codes as needed.

Q: Is datetime.now() time zone aware?
A: No, it returns a naive datetime by default. To get an aware one, pass timezone.utc or another tzinfo object.

Q: Why is datetime.utcnow() deprecated?
A: Because it returns a naive UTC time, which can be misinterpreted as local time. The replacement datetime.now(timezone.utc) is explicit about the time zone.

Q: How do I get the current Unix timestamp?
A: Use time.time() or datetime.now().timestamp(). The latter returns a float with microsecond precision.

Q: Can I get the date and time in a different time zone?
A: Yes, use datetime.now(timezone.utc).astimezone() to convert to local, or specify a timezone object like timezone(timedelta(hours=5, 30)) for IST.

Related

Formula used
in t | Current timestamp (seconds since epoch) | s | 1700000000 | 0
out dt | Local date and time | - | datetime.fromtimestamp(t) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.