JavaScript timestamp: get, generate, and convert epoch time

Tech reviewed: Deepak Prasad
JavaScript timestamp: get, generate, and convert epoch time

People ask for the same data in different word orders: javascript timestamp, js timestamp, timestamp javascript, javascript get timestamp, get timestamp js, or javascript generate timestamp. In modern ECMAScript you almost always start from the built-in Date object (milliseconds since the Unix epoch in UTC) or from Date.now() for the current wall clock. Strings such as ISO-8601 come from toISOString() / toJSON(), while analytics code often wants integer Unix seconds from dividing milliseconds by 1000 and flooring—see Math.floor if you need a refresher.

The runnable snippets below pin one instant in time—2026-06-10T12:00:00.123Z—so the printed numbers stay identical every time you run the examples. In production you would normally call new Date() or Date.now() without arguments; the APIs are the same, only the wall clock changes.

Verified with Node.js v20.18.2: All code samples and commands in this article were executed successfully.


Milliseconds with getTime()

getTime() returns the UTC millisecond count for that Date instance.

javascript
const T = new Date(Date.UTC(2026, 5, 10, 12, 0, 0, 123));
console.log(T.getTime());
text
1781092800123

Date.now() lines up with new Date().getTime()

For the live clock, Date.now() is specified to match the same millisecond value you would get from new Date().getTime() taken in the same instant. A quick sanity check:

javascript
console.log(Math.abs(Date.now() - new Date().getTime()) < 100);
text
true

valueOf() and unary + on a Date

Coercing a Date to a number uses ToPrimitive with a hint of number, which ends at the same millisecond value.

javascript
const T = new Date(Date.UTC(2026, 5, 10, 12, 0, 0, 123));
console.log(T.valueOf());
console.log(+T);
text
1781092800123
1781092800123

Unix seconds (timestamp in seconds)

APIs that expect “Unix time” in seconds divide by 1000 and floor. Watch out for floating-point drift on huge numbers; Math.floor is the usual guard.

javascript
const T = new Date(Date.UTC(2026, 5, 10, 12, 0, 0, 123));
console.log(Math.floor(T.getTime() / 1000));
text
1781092800

ISO string with toISOString()

ISO strings are ideal for logs and JSON payloads that must be human-readable and time-zone-normalized (Z means UTC).

javascript
const T = new Date(Date.UTC(2026, 5, 10, 12, 0, 0, 123));
console.log(T.toISOString());
text
2026-06-10T12:00:00.123Z

JSON-friendly string with toJSON()

Date.prototype.toJSON delegates to toISOString(), which is why serializers produce the same shape.

javascript
const T = new Date(Date.UTC(2026, 5, 10, 12, 0, 0, 123));
console.log(T.toJSON());
text
2026-06-10T12:00:00.123Z

High resolution: performance.now()

performance.now() measures monotonic elapsed time with sub-millisecond precision for the current navigation or Node process; it is not a calendar timestamp. In Node you still have the API on the global object:

javascript
console.log(typeof performance.now);
text
function

Use it for frame timing or benchmarks; pair it with Date.now() when you also need calendar time.


Where to go next

For heavier calendars in apps, many teams now prefer smaller libraries (for example Day.js) or the upcoming Temporal API rather than legacy Moment stacks.


Summary

Timestamp in javascript usually means one of three outputs: epoch milliseconds from Date.now() or getTime(), whole seconds via Math.floor(ms / 1000), or normalized ISO strings from toISOString() for APIs that expect RFC 3339 text. Teams ask whether Date.now() differs from new Date().getTime()—in the same turn of the event loop they represent the same instant, so pick whichever reads clearer in your module.

The other frequent FAQ is when to use performance.now() instead of wall-clock dates: reserve it for monotonic duration measurements (animations, benchmarks), not for user-visible timestamps, because it is not tied to civil time or time zones. Always document whether your service expects seconds or milliseconds, because off-by-three-orders bugs are still extremely common at integration boundaries.


References

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design