October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How Do Python and JavaScript Represent Unix Timestamps?

Convert date-time values to Unix timestamps without timezone or unit mistakes. See Python and JavaScript examples for seconds, milliseconds, and UTC handling.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 Z or 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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:

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.

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:

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 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.Support on Ko-Fi

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.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.