JavaScript Date to Unix Timestamp — Complete Guide With Examples
To convert a JavaScript Date to a Unix timestamp, use Math.floor(Date.now() / 1000) for the current time in seconds. For a specific date, use Math.floor(new Date('2026-07-17').getTime() / 1000). Always divide by 1000, because JavaScript's Date methods return milliseconds, not seconds.
Why Does JavaScript Return Milliseconds Instead Of Seconds?
Every JavaScript Date method that returns a numeric timestamp — Date.now(), getTime(), and valueOf() — returns the count in milliseconds elapsed since the Unix epoch of January 1 1970 00:00:00 UTC. This is a language-level design choice that differs from most server-side languages like PHP and Python, which default to seconds. Anywhere else expecting a standard 10-digit Unix timestamp, including the Unix Timestamp Converter and Discord's timestamp tags, requires that millisecond value divided by 1000.
How Do You Get The Current Unix Timestamp In JavaScript?
Use Date.now() for the fastest current-time read, then convert to seconds:
const unixTimestamp = Math.floor(Date.now() / 1000);
Date.now() is a static method called directly on the Date object, not on a Date instance — calling it as myDate.now() returns undefined rather than a timestamp.
Modern browsers round Date.now() to reduce fingerprinting precision by default. If you need sub-millisecond timing for performance measurement rather than a calendar timestamp, use performance.now() instead.
How Do You Convert A Specific Date To A Unix Timestamp?
Construct a Date object from the date string or components, then apply the same division:
const specificDate = new Date('2026-07-17T00:00:00Z');
const unixTimestamp = Math.floor(specificDate.getTime() / 1000);
The Z at the end of the date string forces UTC interpretation. Omitting it causes the browser or Node.js runtime to parse the date in the local timezone, which shifts the resulting timestamp by your UTC offset.
const localDate = new Date('2026-07-17');
const utcDate = new Date('2026-07-17T00:00:00Z');
How Do You Convert A Unix Timestamp Back To A JavaScript Date?
Multiply the seconds-based timestamp by 1000 before passing it into the Date constructor, since the constructor expects milliseconds:
const date = new Date(1784123456 * 1000);
Skipping the multiplication is the most common reversed version of the same mistake — passing a raw seconds value into new Date() produces a date in January 1970 rather than the intended year.
What Is The Difference Between getTime() And valueOf()?
Both methods return the identical millisecond value, and getTime() is generally preferred in code because its name states its purpose explicitly. valueOf() exists because JavaScript calls it implicitly during arithmetic and comparison operations on Date objects, meaning date1 - date2 works without calling getTime() on either side and returns the millisecond difference directly.
const diffMs = date1 - date2;
const diffSeconds = Math.floor(diffMs / 1000);
How Do You Use A JavaScript Unix Timestamp In A Discord Message?
Discord timestamp tags require the seconds value, matching the standard 10-digit format, not the 13-digit millisecond value JavaScript produces natively. Always run the division before inserting the value into a Discord timestamp tag:
<t:1784123456:F>
Since January 2026, Discord also supports a direct @time mention shortcut for quick single-line timestamps without building a tag manually — useful when scripting Discord bot output isn't necessary and a manual mention is faster.
When generating Discord timestamp codes from a JavaScript backend or bot, wrap the conversion in a single reusable function so the divide-by-1000 step never gets missed in a new code path later.
Frequently Asked Questions
Ready to generate Discord timestamps?
Open the Generator