Python timestamps

datetime and Unix time, without the timezone trap. Rule one: bare fromtimestamp() returns local time.

[MOD.DEMO // TRY]

TRY IT — PASTE A STAMP

—

Now, in seconds

import time
now_s = int(time.time())  # 10 digits

Now, in milliseconds

now_ms = int(time.time() * 1000)  # for JS interop

Stamp to aware datetime (the correct way)

from datetime import datetime, timezone
dt = datetime.fromtimestamp(ts, tz=timezone.utc)  # always pass tz!

Datetime back to stamp

ts = int(dt.replace(tzinfo=timezone.utc).timestamp())  # naive datetimes read as LOCAL — set tz first

JS milliseconds into Python

dt = datetime.fromtimestamp(ms / 1000, tz=timezone.utc)

THE TIMEZONE PITFALL

fromtimestamp(ts) without tz uses your server local zone: +8 in Shanghai, +1 in Berlin, so logs from different regions disagree by hours. Fix it once: always pass tz=timezone.utc and convert with .astimezone() only for display. Naive datetimes handed to .timestamp() are likewise read as local — attach tzinfo first.

[DOC.FAQ // INDEX]

ANSWERS

05 ITEMS
01 How do I get the current Unix timestamp in Python?

int(time.time()) for seconds, int(time.time()*1000) for milliseconds. time.time() returns float seconds since 1970 UTC — truncating with int() gives the standard epoch value other systems expect.

02 Why is fromtimestamp off by hours?

Bare datetime.fromtimestamp(ts) interprets the stamp in local time. Pass tz=timezone.utc explicitly, then .astimezone() for display. The stamp itself carries no zone — only the formatting does.

03 How do I convert JS Date.now() in Python?

Divide by 1000 first: datetime.fromtimestamp(ms/1000, tz=timezone.utc). JS produces 13-digit milliseconds while Python expects seconds; skipping the division lands you in the year 56,000+.

04 How do I convert a datetime back to a stamp?

Aware datetimes: int(dt.timestamp()). Naive datetimes: call dt.replace(tzinfo=timezone.utc) first, otherwise .timestamp() reads them as local and shifts by your UTC offset. Store UTC, display local.

05 Seconds or milliseconds for storage?

Seconds (10 digits) unless you need sub-second precision — every backend speaks seconds natively, and float milliseconds lose precision past 2^53 in JSON. Keep milliseconds only at API edges facing JavaScript.

[DOC.COMPARE]

HOW THIS COMPARES

CapabilityEpoch (this site)EpochConverter
Full Python guide with timezone pitfalls YES Snippets only
Copy-paste aware-datetime patterns YES YES
Live try-it demo on the page YES NO
Covers ms/µs/ns interop with JS & Go YES Partial
Free, no account YES YES

Observed October 2026 against epochconverter.com. If anything changed, tell us and we will correct it.

KEEP EXPLORING TOOLKITS

[DOC.LINKS]

ALSO WORTH A LOOK

Independent alternatives, listed for cross-checking. Presence here is not an endorsement, and corrections are welcome.