A Unix timestamp is the number of seconds since 00:00:00 UTC on 1 January 1970. It's compact, easy to compare and free of time zones, which is why APIs, databases, logs and JWTs use it. The main source of bugs in JavaScript is that Date works in milliseconds while most other systems use seconds.
Get the current timestamp
Date.now(); // 1792656000000 milliseconds
Math.floor(Date.now() / 1000); // 1792656000 seconds
Use Math.floor, not Math.round: rounding can produce a time half a second in the future, which breaks checks like "issued at must not be in the future".
Seconds or milliseconds?
The length gives it away for any date in recent decades:
| Digits | Unit | Example |
|---|---|---|
| 10 | Seconds | 1792656000 |
| 13 | Milliseconds | 1792656000000 |
| 16 | Microseconds | 1792656000000000 |
| 19 | Nanoseconds | 1792656000000000000 |
Passing seconds to new Date() gives a date in January 1970, a sure sign of a unit mix-up:
new Date(1792656000).toISOString(); // '1970-01-21T17:57:36.000Z' wrong
new Date(1792656000 * 1000).toISOString(); // correct
A helper that accepts either unit:
const toDate = (ts) => new Date(Math.abs(ts) < 1e11 ? ts * 1000 : ts);
The threshold of 10^11 treats anything up to the year 5138 in seconds as seconds, and anything after March 1973 in milliseconds as milliseconds. That covers real-world data, but if you control the API, name the field so the unit is explicit, like createdAtMs.
Timestamp to date
const date = new Date(1792656000 * 1000);
date.toISOString(); // UTC, ISO 8601: '2026-10-22T...Z'
date.toUTCString(); // 'Thu, 22 Oct 2026 ... GMT'
date.toLocaleString(); // the user's local time and format
date.toLocaleString("en-GB", { timeZone: "Asia/Kolkata", dateStyle: "medium", timeStyle: "short" });
toISOString() is the right format for APIs and logs. For display, toLocaleString or Intl.DateTimeFormat with an explicit timeZone avoids hand-built strings.
Date to timestamp
const ms = new Date("2026-10-22T09:30:00Z").getTime();
const seconds = Math.floor(ms / 1000);
// Specific UTC date from parts (months are 0-based)
Date.UTC(2026, 9, 22, 9, 30) / 1000;
Parsing pitfalls
Strings without a time zone
The same-looking strings are parsed differently:
new Date("2026-10-22"); // date-only ISO: treated as UTC midnight
new Date("2026-10-22T09:30"); // date-time without offset: treated as LOCAL time
new Date("2026-10-22T09:30Z"); // explicit UTC
Always include Z or an offset like +05:30 in timestamps you store or send.
Non-ISO formats
new Date("10/22/2026") and new Date("22 Oct 2026") work in most engines, but the specification doesn't guarantee non-ISO formats. Parse user-entered dates explicitly, or with a date library.
Months start at 0
new Date(2026, 10, 22) is 22 November. Only the month is zero-based.
Invalid dates don't throw
const d = new Date("not a date");
Number.isNaN(d.getTime()); // true: check before using it
Date arithmetic
Adding seconds or milliseconds to a timestamp is safe for durations ("expires in 15 minutes"):
const expiresAt = Math.floor(Date.now() / 1000) + 15 * 60;
For calendar arithmetic ("same time tomorrow", "first of next month") use date methods in the right time zone, because days aren't always 24 hours long when daylight saving changes. The newer Temporal API handles both cases explicitly where available.
In other languages and databases
import time, datetime
int(time.time()) # now, seconds
datetime.datetime.fromtimestamp(1792656000, tz=datetime.timezone.utc) # to aware datetime
int(datetime.datetime(2026, 10, 22, tzinfo=datetime.timezone.utc).timestamp())
-- PostgreSQL
SELECT extract(epoch FROM now())::bigint;
SELECT to_timestamp(1792656000);
-- MySQL
SELECT UNIX_TIMESTAMP();
SELECT FROM_UNIXTIME(1792656000);
date +%s # now, seconds
date -u -d @1792656000 # GNU date
date -u -r 1792656000 # macOS / BSD date
Where you'll meet timestamps
- JWTs:
exp,iatandnbfare seconds. Comparing them withDate.now()without dividing by 1000 makes every token look valid for thousands of years. See JWT decode vs verify. - Cron and schedulers: run times are usually logged in UTC; the cron cheat sheet covers time zone settings.
- Time-ordered IDs: a UUID v7 begins with a millisecond timestamp.