Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To convert a datetime to a Unix timestamp, first make sure it identifies the intended instant and timezone, then count from 1970-01-01 00:00:00 UTC. Check the required unit: Python’s datetime.timestamp() returns seconds, while JavaScript’s Date uses milliseconds.

What a Unix timestamp represents

A Unix timestamp, also called an epoch timestamp, measures time from 1970-01-01 00:00:00 UTC. It represents an instant, not a timezone or a formatted calendar date. The timezone matters when interpreting the original datetime; once the instant is established, the timestamp is a count from the fixed UTC epoch.

Before converting, establish three things: what moment the input means, whether the destination expects seconds or milliseconds, and how it should handle any fractional second.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Convert a Python datetime to a Unix timestamp

For a timezone-aware datetime, call timestamp(). It returns POSIX seconds as a floating-point number.

from datetime import datetime, timezone

dt = datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
unix_seconds = dt.timestamp()

Python defines this calculation relative to an aware UTC epoch. A naive datetime has no timezone information; Python assumes it represents the system’s local time when timestamp() is called. If the value is meant to be UTC, make that convention explicit rather than relying on the machine’s local timezone. Python notes that there is no direct method to obtain a POSIX timestamp from a naive datetime representing UTC.

Attach UTC only when the naive value is already UTC

If your input contract says a naive datetime’s clock fields are UTC, you can attach UTC metadata before converting:

from datetime import timezone

unix_seconds = naive_dt.replace(tzinfo=timezone.utc).timestamp()

replace() assigns timezone information; it does not convert clock fields. If the datetime represents local time or another timezone, interpret it in that actual zone before obtaining its timestamp.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose how to handle fractional seconds

Keep the float if the receiving system accepts fractional seconds. If it requires an integer, select a policy that matches its contract. For example, int(dt.timestamp()) truncates toward zero; for values before the epoch, that differs from taking the floor. Do not discard precision or round implicitly when the consumer specifies a different rule.

Convert a timestamp back to an aware UTC datetime

To get a timezone-aware UTC result, pass tz=timezone.utc:

from datetime import datetime, timezone

utc_dt = datetime.fromtimestamp(unix_seconds, tz=timezone.utc)

Without the tz argument, Python returns a naive datetime in the system’s local timezone.

Convert a JavaScript Date to Unix seconds

JavaScript Date represents a moment as milliseconds from the epoch. Divide by 1,000 to express it in seconds. For whole elapsed seconds rounded down, use:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const unixSeconds = Math.floor(date.getTime() / 1000);

If the destination expects milliseconds, use date.getTime() directly. If it expects an integer number of seconds but requires truncation or rounding instead of flooring, apply that explicit policy.

Create a timestamp from UTC calendar components

Date.UTC() accepts UTC calendar components and 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.Support on Ko-Fi

Parse the input without changing its meaning

Parsing can introduce a timezone shift before the conversion even happens. Use an input that states its offset or timezone whenever the value is meant to identify an instant.

  • Python: A naive datetime passed to timestamp() is treated as local time. Do not treat it as UTC unless that is the documented input convention.
  • JavaScript date-only strings: In the standardized date-time string format, a string such as "2024-01-01" is interpreted as UTC.
  • JavaScript date-time strings without an offset: A string such as "2024-01-01T00:00:00" is interpreted as local time. Add Z for UTC or provide an explicit offset when the input identifies an instant.
  • Non-standard JavaScript strings: Their parsing can vary between implementations, including in timezone assumptions. Prefer the standardized format with an explicit offset.

UTC conversion and local display are separate operations. The timestamp identifies the instant; a timezone determines how that instant appears as local date and time.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the unit, precision, and sign

  • Seconds versus milliseconds: Python’s timestamp() returns seconds; JavaScript Date uses milliseconds. A factor-of-1,000 discrepancy usually means the value was passed in the wrong unit.
  • Fractional seconds: Python can return a float. JavaScript’s getTime() returns milliseconds, which may include a fractional number of seconds when divided by 1,000. Preserve or reduce precision according to the destination’s requirements.
  • Before 1970: Instants before the epoch have negative timestamps. Apply a deliberate rounding or truncation rule if converting those values to whole seconds.

Quick decision checklist

  1. Identify whether the input includes UTC, an explicit offset, a named timezone, or no timezone at all.
  2. Resolve any timezone-free input according to its documented convention before converting.
  3. Confirm whether the receiving system expects seconds, milliseconds, or fractional seconds.
  4. Choose whether to preserve, floor, truncate, or round fractional seconds.
  5. For a displayed date, convert the instant to the desired timezone separately from calculating the timestamp.

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.