Quick Answers: Python Timestamp
- time.time() gives Unix seconds. Wrap it in int() for a whole number, or use time.time_ns() for nanoseconds.
- Always pass a time zone to fromtimestamp. Without one you get a naive datetime in local time, which is easy to misuse.
- datetime.timestamp() goes the other way, but only when the datetime is aware. A naive datetime is read as local time.
- ZoneInfo handles time zones properly. It reads the same database browsers use, so daylight saving follows the real rules.
Python Counts Seconds, Like Unix Does
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
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
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:00The 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
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())) # 1715429912Build 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
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:00ZoneInfo 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
- Using a naive datetime. datetime.now() with no zone is naive. Attach timezone.utc or a ZoneInfo zone at creation, not later.
- Forgetting the zone on fromtimestamp. datetime.fromtimestamp(1715429912) without a zone gives local time. Pass timezone.utc for the UTC reading.
- Mixing naive and aware datetimes. Subtracting one from the other raises a TypeError. Make both aware first.
- Using utcnow(). datetime.utcnow() is deprecated because it returns a naive datetime that looks like UTC but is not marked as such. Use datetime.now(timezone.utc) instead.
- Storing datetimes as text without a zone. A bare 2024-05-11 12:18:32 in a database cannot be converted back reliably. Store the timestamp, or store the ISO string with its offset.
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.