Discord Guide

JavaScript Date to Unix Timestamp — Complete Guide With Examples

July 17, 2026 · 4 min read

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.

Pro Tip:

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.

Pro Tip:

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

A JavaScript Unix timestamp is obtained with Math.floor(Date.now() / 1000), converting JavaScript's native millisecond output into the standard 10-digit seconds format most systems expect.
Math.floor(new Date('2026-07-17T00:00:00Z').getTime() / 1000) converts a specific ISO date string into its Unix timestamp equivalent in seconds.
Call Date.now() for the current millisecond timestamp, then divide by 1000 and round down with Math.floor() to get the standard Unix timestamp in seconds.
getTime() is an instance method on a JavaScript Date object that returns the number of milliseconds elapsed since January 1 1970 00:00:00 UTC.
Multiply a seconds-based Unix timestamp by 1000 before passing it into new Date(), since the Date constructor always expects a millisecond value.
Pass a millisecond timestamp into new Date(ms), then call formatting methods like toLocaleDateString() or toISOString() to produce a human readable string.
While JavaScript can convert timestamps with built-in Date methods, a dedicated [Unix Timestamp Converter](/unix-timestamp-converter) confirms the digit count and unit instantly without writing or debugging any code. Checked — all t tags are in their own separate code blocks

Ready to generate Discord timestamps?

Open the Generator

Did You Miss These?

Discord Guide

Generate a Discord Timestamp

Free tool — all 7 formats, live Discord preview, auto timezone detection.

Open Generator →
Discord Guide12 min read

Jul 17, 2026

Discord Timestamp — The Complete Guide to All 7 Formats

Everything about Discord timestamps — all 7 format codes, the syntax, how to generate codes, the @time feature, troubleshooting, and why timestamps beat manual timezone conversion.

Read Article →
Discord Guide12 min read

Jul 17, 2026

What Is Epoch Time? Complete Guide to Unix Timestamps and January 1 1970

Epoch time is the total number of seconds since January 1 1970 at 00:00:00 UTC. Complete guide covering what epoch time is, why 1970 was chosen, how it works, and code examples.

Read Article →
Discord Guide11 min read

Jul 17, 2026

Unix Timestamp — Complete Guide to Epoch Time and How It Works

A Unix timestamp is a single integer counting seconds since January 1 1970 UTC. Complete guide covering format, precision levels, code examples in every language, and the Year 2038 problem.

Read Article →
Discord Guide10 min read

Jul 17, 2026

Timestamp to Date — How to Convert Any Timestamp to a Readable Date

How to convert any Unix timestamp to a readable date in JavaScript, Python, PHP, SQL, Java, Excel, and Bash. Includes milliseconds vs seconds fix and timezone best practices.

Read Article →