Question
I want to get the current date and time in JavaScript as a single numeric value, similar to a Unix timestamp. What is the correct way to do that, and what format does JavaScript return by default?
Short Answer
By the end of this page, you will understand how JavaScript represents timestamps, how to get the current time as a number, and when to use milliseconds versus seconds. You will also see practical examples, common mistakes, and a small project that uses timestamps in real code.
Concept
JavaScript can represent the current date and time as a timestamp, which is a single number.
A timestamp is useful because:
- it is easy to store in databases
- it is easy to compare
- it is easy to subtract to measure elapsed time
- it avoids many formatting issues that happen with date strings
In JavaScript, the most common timestamp value is the number of milliseconds since January 1, 1970 UTC. This moment is called the Unix epoch.
The simplest way to get the current timestamp is:
Date.now()
Example:
const timestamp = Date.now();
console.log(timestamp);
This returns something like:
1715000000000
That number is in milliseconds, not seconds.
If you want a Unix timestamp in seconds, divide by 1000 and usually round down:
const unixSeconds = Math.floor(Date.now() / 1000);
console.log(unixSeconds);
This matters because many APIs, databases, and other programming languages use Unix time in seconds, while JavaScript commonly uses milliseconds.
Mental Model
Think of a timestamp like a stopwatch that started at a fixed point in history: January 1, 1970 UTC.
JavaScript asks: "How many tiny time units have passed since that starting point?"
Date.now()gives the count in milliseconds- Unix timestamps often use seconds
So the timestamp is not a formatted date like 2025-01-10. It is more like a raw counter value that computers can work with quickly.
Another way to picture it:
- a date string is like a human-readable calendar page
- a timestamp is like the serial number for that moment in time
Syntax and Examples
The core syntax in JavaScript is:
Date.now()
This returns the current time in milliseconds.
Example 1: Current timestamp in milliseconds
const now = Date.now();
console.log(now);
Possible output:
1715001234567
Example 2: Current Unix timestamp in seconds
const unixTimestamp = Math.floor(Date.now() / 1000);
console.log(unixTimestamp);
Possible output:
1715001234
Example 3: Using new Date().getTime()
now = ().();
.(now);
Step by Step Execution
Consider this code:
const ms = Date.now();
const seconds = Math.floor(ms / 1000);
console.log(ms);
console.log(seconds);
Step by step:
Date.now()gets the current time as the number of milliseconds since the Unix epoch.- That value is stored in
ms. ms / 1000converts milliseconds into seconds.Math.floor(...)removes the decimal part so the result becomes a whole-number Unix timestamp.console.log(ms)prints the millisecond timestamp.console.log(seconds)prints the second-based Unix timestamp.
Example trace:
- suppose
Date.now()returns1715001234567 - divide by
1000→1715001234.567
Real World Use Cases
Timestamps are used in many real programs.
Logging events
console.log(`Request received at ${Date.now()}`);
Applications often record exactly when something happened.
Measuring elapsed time
const start = Date.now();
// some work here
const end = Date.now();
console.log(`Took ${end - start} ms`);
This is useful for performance checks.
Saving creation times
const user = {
name: "Ava",
createdAt: Date.now()
};
Many apps store when a record was created or updated.
Expiration logic
const expiresAt = .() + * * ;
Real Codebase Usage
In real codebases, developers use timestamps in patterns like these:
Audit fields
Objects and database records often include fields such as:
createdAtupdatedAtdeletedAt
Example:
const post = {
title: "Hello",
createdAt: Date.now(),
updatedAt: Date.now()
};
Guard clauses for expiration
function isExpired(expiresAt) {
if (Date.now() > expiresAt) {
return true;
}
return false;
}
This is common in authentication and caching.
Validation of incoming data
function isValidTimestamp() {
.(value) && value > ;
}
Common Mistakes
Mistake 1: Confusing milliseconds with seconds
JavaScript usually gives timestamps in milliseconds.
Broken assumption:
const timestamp = Date.now();
// Assuming this is already in seconds
Fix:
const unixSeconds = Math.floor(Date.now() / 1000);
Mistake 2: Forgetting to round when converting to seconds
const seconds = Date.now() / 1000;
console.log(seconds); // has decimals
If you need a standard whole-number Unix timestamp:
const seconds = Math.floor(Date.now() / 1000);
Mistake 3: Using new Date() when only a number is needed
Comparisons
| Approach | Returns | Type | Best use |
|---|---|---|---|
Date.now() | Current time in milliseconds | number | Best way to get a numeric timestamp now |
new Date().getTime() | Current time in milliseconds | number | Works, but more verbose |
new Date() | Current date/time object | object | Use when you need date methods |
Math.floor(Date.now() / 1000) | Current time in seconds | number |
Cheat Sheet
// Current timestamp in milliseconds
Date.now()
// Current timestamp in seconds
Math.floor(Date.now() / 1000)
// Equivalent to Date.now(), but longer
new Date().getTime()
// Convert timestamp to Date object
new Date(timestamp)
Quick rules
- JavaScript timestamps are usually in milliseconds.
- Unix timestamps are often in seconds.
- Use
Date.now()for the current numeric timestamp. - Use
Math.floor(Date.now() / 1000)for whole Unix seconds. - Use
new Date(timestamp)to turn a timestamp into a date object.
Common edge case
If an API returns seconds and you pass it directly to new Date(...), the result may be wrong because new Date(...) usually expects milliseconds.
Fix:
const seconds = ;
date = (seconds * );
FAQ
How do I get the current Unix timestamp in JavaScript?
Use:
Math.floor(Date.now() / 1000)
This returns the current Unix timestamp in seconds.
Does Date.now() return seconds or milliseconds?
Date.now() returns milliseconds since January 1, 1970 UTC.
What is the difference between Date.now() and new Date()?
Date.now()returns a numbernew Date()returns aDateobject
Is new Date().getTime() the same as Date.now()?
Yes. Both return the current timestamp in milliseconds.
Why is my timestamp so large in JavaScript?
Because JavaScript usually uses milliseconds, which produces larger numbers than second-based Unix timestamps.
How do I convert a JavaScript timestamp to a readable date?
Use:
Mini Project
Description
Build a simple event logger that records when actions happen using JavaScript timestamps. This demonstrates how to create numeric timestamps, convert them to readable dates, and measure elapsed time between events.
Goal
Create a small script that stores events with timestamps and prints both the raw timestamp and a human-readable date.
Requirements
- Create a function that logs an event name with the current timestamp.
- Store each event in an array as an object.
- Print each event's name, raw timestamp, and readable date.
- Show how long ago the previous event happened in milliseconds.
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.