Python · datetime · Live tool below

Python Timestamp
Unix time with datetime and time.

Convert unix timestamps in Python: current time with time.time(), seconds to datetime, dates back to timestamps, and time zones with ZoneInfo. The live converter sits at the top, and every snippet below was run and checked.

Live ToolAll processing local. No data sent.
● --
Waiting for inputSeconds, milliseconds, microseconds and nanoseconds are detected from the digit count.

Quick Answers: Python Timestamp

Python Counts Seconds, Like Unix Does

Quick answer: int(time.time()) is the unix timestamp in seconds. No multiplication, no division, and no millisecond trap like JavaScript.

Python counts time the same way Unix does: in seconds since the epoch. time.time() returns that count as a float, so int(time.time()) is the 10 digit unix timestamp, and time.time_ns() is the same instant in nanoseconds, 19 digits long. That makes Python one of the easiest languages for timestamp work, because the value coming out of the standard library is already the value most databases and APIs expect. The only trap is the one Python sets for itself: datetimes without a time zone.

The converter above shows the same numbers live, and it detects all four precisions from the digit count, so a time_ns() value pasted in is recognised as nanoseconds at once, not mistaken for a date far in the future.

Current Unix Timestamp in Python

Python
import time

# Current Unix timestamp in seconds
now = int(time.time())

# Current timestamp in nanoseconds (19 digits)
now_ns = time.time_ns()

int(time.time()) is the standard way to read the clock in Python. The float behind it carries fractional seconds, which is useful for measuring short intervals, and time.time_ns() gives the same instant as an integer count of nanoseconds, which avoids float rounding entirely. For logging, the seconds value is the one to store: it is 10 digits, it sorts as a number, and every other language on this site reads it the same way.

Unix Timestamp to Date in Python

Python
from datetime import datetime, timezone

# Unix seconds to date, always with a zone
dt = datetime.fromtimestamp(1715429912, timezone.utc)
print(dt)  # 2024-05-11 12:18:32+00:00

print(dt.isoformat())  # 2024-05-11T12:18:32+00:00

The reference value used across this site is 1715429912, which is Saturday, 11 May 2024 at 12:18:32 UTC. datetime.fromtimestamp with timezone.utc gives 2024-05-11 12:18:32+00:00, and isoformat() gives the ISO 8601 reading 2024-05-11T12:18:32+00:00. The timezone argument is the whole lesson of this page: without it, fromtimestamp returns a naive datetime in your local time, which prints like a normal date but carries no zone, and mixing it with an aware datetime later raises an error.

Date to Unix Timestamp in Python

Python
from datetime import datetime, timezone

# Date to Unix timestamp, always with a zone
dt = datetime(2024, 5, 11, 12, 18, 32, tzinfo=timezone.utc)
print(int(dt.timestamp()))  # 1715429912

# Parse an ISO 8601 string (Python 3.11+ reads Z directly)
dt2 = datetime.fromisoformat('2024-05-11T12:18:32Z')
print(int(dt2.timestamp()))  # 1715429912

Build the datetime with tzinfo=timezone.utc and call .timestamp() to get 1715429912.0, then wrap it in int() for the whole seconds. datetime.fromisoformat parses an ISO string the same way, and Python 3.11 and later reads the Z designator directly. In older versions, replace the Z with +00:00 first. The converter at the top of this page accepts the same ISO strings, so you can check a parsing result there before trusting it in code.

Python Timestamps in Other Time Zones

Python
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

dt = datetime.fromtimestamp(1715429912, timezone.utc)

# The same instant in other zones
print(dt.astimezone(ZoneInfo('Asia/Karachi')))   # 2024-05-11 17:18:32+05:00
print(dt.astimezone(ZoneInfo('America/New_York')))  # 2024-05-11 08:18:32-04:00

ZoneInfo is built into Python 3.9 and later, and it reads the IANA time zone database, the same database browsers use. astimezone converts an aware datetime to any zone, and the offset printed beside the result is the one that applies to that exact date, with daylight saving handled by the real rules. The timezone converter on this site shows the same instant in 12 zones at once, so you can check a ZoneInfo result against it in seconds.

Common Python Timestamp Mistakes

Python Timestamp FAQs

How do I get the current unix timestamp in Python?

Use int(time.time()). It returns the current time in seconds since the epoch, as an integer. If you need fractions, time.time() alone returns a float with the fractional seconds included. For the highest precision, time.time_ns() returns nanoseconds as an integer, 19 digits long. All three read the same clock, so they agree with each other and with the live converter above.

How do I convert a unix timestamp to a date in Python?

Use datetime.fromtimestamp with the UTC time zone: datetime.fromtimestamp(1715429912, timezone.utc) gives 2024-05-11 12:18:32+00:00. Always pass the timezone. Without it, fromtimestamp returns a naive datetime in your local time, which has no zone attached and is easy to misuse later. The isoformat() method then gives 2024-05-11T12:18:32+00:00, the ISO 8601 reading.

How do I convert a date to a unix timestamp in Python?

Build an aware datetime and call timestamp(): datetime(2024, 5, 11, 12, 18, 32, tzinfo=timezone.utc).timestamp() gives 1715429912.0. The tzinfo part is the step people skip. A naive datetime without it is read in local time, so the same code gives a different timestamp in Karachi than in London. Attach timezone.utc for UTC, or a ZoneInfo zone for any other place.

What is the difference between time.time() and datetime.now() in Python?

time.time() returns the current time as a float of seconds since the epoch, which is the unix timestamp itself. datetime.now() returns a datetime object, which is useful for formatting and arithmetic but needs a timezone to be safe. For storing, comparing or sending a time, use the timestamp from time.time(). For showing it to a person, convert it with datetime.fromtimestamp and a timezone.

What is a naive datetime in Python, and why is it dangerous?

A naive datetime has no tzinfo, so Python does not know which time zone it belongs to. It prints like a normal date, which makes the danger invisible: arithmetic between a naive datetime and an aware one raises an error, and converting a naive datetime to a timestamp silently assumes local time. The fix is simple. Always attach a zone when you create a datetime, with tzinfo=timezone.utc for UTC or ZoneInfo('Asia/Karachi') for another zone.

How do I handle time zones in Python?

Use zoneinfo.ZoneInfo, which is built into Python 3.9 and later. datetime.fromtimestamp(1715429912, ZoneInfo('Asia/Karachi')) gives 2024-05-11 17:18:32+05:00, with the offset attached. ZoneInfo reads the same IANA database browsers use, so daylight saving is handled by the real rules. The older pytz library does the same job, but its localize method is easy to misuse, so ZoneInfo is the better default for new code.

Why does Python give 19 digits for time.time_ns()?

Because it counts nanoseconds, billionths of a second, since the epoch. The current value is 19 digits long, which is why the unix timestamp converter on the home page detects 19 digit numbers as nanoseconds. Most code does not need that precision: int(time.time()) in seconds is enough for logs, databases and APIs. Use time_ns() only when you are measuring something that happens in less than a millisecond.

How do I parse an ISO 8601 date in Python?

Use datetime.fromisoformat in Python 3.11 and later, which reads the Z designator directly: datetime.fromisoformat('2024-05-11T12:18:32Z') gives an aware datetime in UTC. In older versions, replace the Z with +00:00 first, because fromisoformat did not accept Z before 3.11. Then call .timestamp() to get 1715429912. For unusual formats, use datetime.strptime with the matching format string instead.

Related tools

Source: Python documentation, datetime and time. Reference values on this page were run in Python 3.12 and checked on 27 November 2026.