Unix Timestamp in JavaScript
JavaScript provides several built-in ways to get and work with Unix timestamps. Here's everything you need.
Get the current Unix timestamp
JavaScript
// Seconds (standard Unix timestamp)
Math.floor(Date.now() / 1000) // e.g. 1700000000
// Milliseconds (used by most JS APIs)
Date.now() // e.g. 1700000000000
// Alternative ways
Math.floor(new Date() / 1000)
new Date().getTime() // milliseconds
+new Date() // milliseconds (unary +)Convert epoch timestamp to Date
JavaScript
// If you have seconds, multiply by 1000 for JS Date
const epoch = 1700000000;
const date = new Date(epoch * 1000);
console.log(date.toISOString()); // "2023-11-14T22:13:20.000Z"
console.log(date.toUTCString()); // "Tue, 14 Nov 2023 22:13:20 GMT"
console.log(date.toLocaleString()); // local time stringConvert Date to epoch timestamp
JavaScript
// From a Date object
const date = new Date('2023-11-14T22:13:20Z');
const epochSeconds = Math.floor(date.getTime() / 1000); // 1700000000
const epochMs = date.getTime(); // 1700000000000
// From a date string
Math.floor(new Date('2026-01-01').getTime() / 1000)
// From specific components (UTC)
Math.floor(Date.UTC(2026, 0, 1, 0, 0, 0) / 1000) // months are 0-indexedFormat epoch as readable date
JavaScript
const d = new Date(1700000000 * 1000);
// ISO 8601 (best for APIs and logs)
d.toISOString() // "2023-11-14T22:13:20.000Z"
// Locale-aware formatting
d.toLocaleString('en-US', { timeZone: 'America/New_York' })
// Custom format using Intl.DateTimeFormat
const fmt = new Intl.DateTimeFormat('en-GB', {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit',
timeZone: 'UTC', hour12: false
});
fmt.format(d); // "14/11/2023, 22:13:20"Add / subtract time from a timestamp
JavaScript
const now = Math.floor(Date.now() / 1000);
const oneHourLater = now + 3600;
const oneDayLater = now + 86400;
const oneWeekLater = now + 604800;
const thirtyDaysAgo = now - (30 * 86400);Check if a timestamp is expired (e.g. JWT)
JavaScript
function isExpired(epochSeconds) {
return Math.floor(Date.now() / 1000) > epochSeconds;
}
isExpired(1700000000); // true — that date is in the pastRelative time ("5 minutes ago")
JavaScript
// Using Intl.RelativeTimeFormat (modern browsers)
function relativeTime(epochSeconds) {
const diff = epochSeconds - Math.floor(Date.now() / 1000);
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
const units = [
['year', 31536000], ['month', 2592000], ['week', 604800],
['day', 86400], ['hour', 3600], ['minute', 60], ['second', 1]
];
for (const [unit, secs] of units) {
if (Math.abs(diff) >= secs) {
return rtf.format(Math.round(diff / secs), unit);
}
}
return 'just now';
}
relativeTime(Date.now() / 1000 - 300); // "5 minutes ago"→ Use the live epoch converter to convert any JavaScript timestamp instantly.