Unix Timestamp in Python

Python's time module gives you the current Unix timestamp as a float (seconds with decimal precision). Use int() to truncate to whole seconds.

Ad

Code Examples

Current timestamp (seconds)

import time
int(time.time())

The classic way. time.time() returns a float; int() truncates to whole seconds. Works in Python 2 and 3.

Current timestamp (milliseconds)

import time
int(time.time() * 1000)

Multiply by 1000 for millisecond precision — useful when interfacing with JavaScript or Java APIs.

Using datetime module

from datetime import datetime, timezone
int(datetime.now(timezone.utc).timestamp())

The datetime approach. Always pass timezone.utc to avoid local-time ambiguity when computing the timestamp.

Convert timestamp to datetime

from datetime import datetime, timezone
datetime.fromtimestamp(timestamp, tz=timezone.utc)

Convert a Unix timestamp back to a timezone-aware datetime object. Omitting tz uses local time, which can cause bugs.

pandas (data science)

import pandas as pd
pd.Timestamp.now().timestamp()

In pandas, Timestamps are timezone-aware and convert cleanly to Unix time.

Note

Python's time.time() returns seconds as a float, not milliseconds. This is the opposite of JavaScript's Date.now(). Be careful when passing timestamps between Python backends and JS frontends.

Ad

Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.

← Open the converter