- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Timestamps
JavaScript Dates
JavaScript Timestamps
A timestamp is the number of milliseconds since the start of 1970.
That moment is called the Unix epoch.
Storing a timestamp avoids every timezone and formatting question.
Example
Example
javascript
const timestamp = Date.now();
console.log(typeof timestamp);
console.log(timestamp > 0);The value changes constantly, so the checks are what matter.
Working with Timestamps
Date.now() gives the current timestamp.
date.getTime() gives the timestamp of a date.
new Date(timestamp) converts one back.
Syntax
Syntax
javascript
Date.now();
new Date(timestamp);JavaScript uses milliseconds; many other systems use seconds.
Converting Both Ways
A timestamp and a Date hold exactly the same information.
Example
Example
javascript
const date = new Date(Date.UTC(2026, 0, 15));
const timestamp = date.getTime();
const back = new Date(timestamp);
console.log(back.getUTCFullYear());
console.log(back.getTime() === timestamp);Nothing is lost in the round trip.
The Epoch
Timestamp zero is the very start of 1970, in UTC.
Example
Example
javascript
const epoch = new Date(0);
console.log(epoch.getUTCFullYear());
console.log(epoch.getUTCMonth());
console.log(epoch.getUTCDate());The output is 1970, 0, 1.
Seconds Versus Milliseconds
Many APIs send seconds, not milliseconds.
Multiplying by 1000 is the fix, and forgetting it lands you in 1970.
Example
Example
javascript
const seconds = 1767225600;
const wrong = new Date(seconds);
const right = new Date(seconds * 1000);
console.log(wrong.getUTCFullYear());
console.log(right.getUTCFullYear());The first is barely past the epoch; the second is the intended date.
Measuring Elapsed Time
Subtracting two timestamps gives the duration.
Example
Example
javascript
const started = Date.now();
let total = 0;
for (let i = 0; i < 100000; i++) {
total += i;
}
const elapsed = Date.now() - started;
console.log(elapsed >= 0);
console.log(typeof elapsed);performance.now() is more precise for timing code.
Sorting by Timestamp
Numbers sort reliably, which makes timestamps ideal for ordering.
Example
Example
javascript
const events = [
{ name: "second", at: Date.UTC(2026, 0, 15) },
{ name: "first", at: Date.UTC(2026, 0, 1) }
];
const sorted = events.slice().sort(function (a, b) {
return a.at - b.at;
});
console.log(sorted.map(function (e) { return e.name; }).join(","));The output is first,second.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Timestamps</title>
</head>
<body>
<h1>Timestamps</h1>
<p id="out"></p>
<script>
const now = Date.now();
const epoch = new Date(0);
const date = new Date(Date.UTC(2026, 0, 15));
document.getElementById("out").innerHTML =
"Now: " + now +
"<br>Epoch year: " + epoch.getUTCFullYear() +
"<br>Fixed date timestamp: " + date.getTime() +
"<br>Back to a date: " + new Date(date.getTime()).toISOString().slice(0, 10);
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try the seconds trap:
Divide the timestamp by 1000 and see the date jump back to 1970.
Important Points
- A timestamp counts milliseconds since 1970.
Date.now()gives the current one.new Date(timestamp)converts it back.- Many other systems use seconds, not milliseconds.
- Timestamps sort reliably as plain numbers.
Conclusion
Storing a timestamp sidesteps timezone and format questions entirely.
The seconds-versus-milliseconds mix-up is the classic bug here.
Convert to a Date only at the moment you need to display it.
