To convert a date-time value to a Unix timestamp, first make sure it identifies the intended instant and timezone, then express the time since 1970-01-01 00:00:00 UTC in the unit your application expects. Python’s datetime.timestamp() returns seconds; JavaScript’s Date uses milliseconds.
Choose the timezone and timestamp unit first
A Unix timestamp represents an instant relative to the Unix epoch: midnight UTC on January 1, 1970. It does not preserve a timezone label or the original calendar representation. A timezone is important when interpreting the input date-time; the output unit is important when passing the result to another system.
As an Amazon Associate I earn from qualifying purchases.
- Identify the instant: Prefer a timezone-aware value or an input string with
Zor an explicit UTC offset. A timezone-free wall-clock time is ambiguous unless your application defines which timezone it uses. - Check the unit: A receiver may expect seconds, milliseconds, or fractional seconds. Python and JavaScript use different defaults.
- Choose precision behavior: If whole seconds are required, decide whether to floor, truncate, or round the fractional part. These choices can differ, especially for times before the epoch.
Convert a Python datetime to Unix seconds
For an aware UTC datetime, call timestamp(). Python returns POSIX seconds as a floating-point number, so fractional seconds may be present. The Python documentation defines the calculation for an aware datetime relative to an aware UTC epoch: Python datetime documentation.
from datetime import datetime, timezone
dt = datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
unix_seconds = dt.timestamp() # float: seconds since the epoch
Handle naive datetimes deliberately
A naive Python datetime has no timezone information. Python’s timestamp() treats it as local time; it does not infer that the value means UTC. If your input convention explicitly says that a naive value is UTC, attach UTC before converting:
#1 Best Overall
from datetime import timezone
unix_seconds = naive_dt.replace(tzinfo=timezone.utc).timestamp()
replace() assigns timezone metadata without changing the clock fields. Do not use it on a value that represents another timezone; attach the correct timezone and convert the intended instant instead. If you need whole seconds, choose a policy explicitly. For example, int(dt.timestamp()) truncates toward zero, which differs from flooring for negative timestamps.
Convert a timestamp back to an aware UTC datetime
Pass tz=timezone.utc to fromtimestamp() when you want an aware UTC result. Without tz, Python returns a naive datetime in the system’s local timezone.
Rank #2
from datetime import datetime, timezone
utc_dt = datetime.fromtimestamp(unix_seconds, tz=timezone.utc)
Convert a JavaScript Date to Unix seconds
JavaScript Date represents an instant as milliseconds from the epoch. Divide by 1,000 to get seconds; use Math.floor() when the required result is whole elapsed seconds:
const unixSeconds = Math.floor(date.getTime() / 1000);
If the receiving system expects milliseconds, use date.getTime() without dividing. If it requires a different rounding policy, apply that policy explicitly rather than assuming flooring is interchangeable with truncation or rounding. See the MDN Date reference.
Create a JavaScript date from UTC calendar components
For UTC components rather than an existing Date, Date.UTC() returns milliseconds. Its month argument is zero-based, so January is 0.
const unixSeconds = Math.floor(
Date.UTC(2024, 0, 1, 0, 0, 0) / 1000
);
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Watch for parsing and timezone surprises
Conversion code can be correct while parsing gives the input a different meaning than intended. In JavaScript’s standardized date-time string format, a date-only string such as "2024-01-01" is interpreted as UTC, while a date-time string without an offset such as "2024-01-01T00:00:00" is interpreted as local time. A string with Z or an explicit offset makes the intended instant clearer. Non-standard date strings can be parsed differently across implementations, so avoid relying on them for portable behavior. See MDN’s Date.parse() reference.
UTC conversion and local display are separate operations: the timestamp identifies an instant, while local timezone rules affect how calendar fields are interpreted or shown. Timestamps for instants before January 1, 1970 are negative; do not assume every valid result is positive.
Recommended Free Tools
Quick Recap
Best Value
Quick reference
| Language and input | Conversion | Result |
|---|---|---|
Python, timezone-aware datetime |
dt.timestamp() |
Seconds as a float |
Python, naive datetime |
Assign the correct timezone before calling timestamp() |
Depends on the timezone you assign; naive values are treated as local by timestamp() |
JavaScript, existing Date |
date.getTime() |
Milliseconds |
JavaScript, existing Date to whole seconds |
Math.floor(date.getTime() / 1000) |
Floored seconds |
| JavaScript, UTC calendar components | Date.UTC(...) |
Milliseconds; divide by 1,000 for seconds |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




