Last updated
Common Mistakes
getMonth()returns 0–11, not 1–12 — always add 1 when displaying- Unix timestamps are in seconds; JavaScript Date expects milliseconds — multiply by 1000
- Local time methods depend on the user's browser timezone — use UTC methods for consistent output
- Avoid Moment.js for new projects — it is in maintenance mode; use date-fns or Luxon instead
Use the Epoch Converter tool to verify your conversions instantly without writing any code.
Examples
Example 1: Basic Conversion with the Date Object
JavaScript timestamps are in milliseconds. Unix timestamps are in seconds. Always multiply by 1000:
// Unix timestamp (seconds) → JavaScript Date
const unixTimestamp = 1710000000; // seconds
const date = new Date(unixTimestamp * 1000);
console.log(date.toString());
// "Sat Mar 09 2024 21:20:00 GMT+0000"
console.log(date.toISOString());
// "2024-03-09T21:20:00.000Z"
console.log(date.toLocaleDateString());
// "3/9/2024" (format depends on browser locale)
Example 2: Manual Date Formatting
Build a custom formatted string from date components:
function formatDate(unixTimestamp) {
const date = new Date(unixTimestamp * 1000);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // getMonth() is 0-indexed!
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const mins = String(date.getMinutes()).padStart(2, '0');
const secs = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${mins}:${secs}`;
}
console.log(formatDate(1710000000));
// "2024-03-09 21:20:00"
Example 3: Locale-Aware Formatting with Intl.DateTimeFormat
Use the built-in Intl API for locale-specific formatting without any library:
const date = new Date(1710000000 * 1000);
// US format
new Intl.DateTimeFormat('en-US').format(date);
// "3/9/2024"
// German format
new Intl.DateTimeFormat('de-DE').format(date);
// "9.3.2024"
// Full date and time
new Intl.DateTimeFormat('en-US', {
year: 'numeric', month: 'long', day: 'numeric',
hour: '2-digit', minute: '2-digit', second: '2-digit',
timeZoneName: 'short'
}).format(date);
// "March 9, 2024 at 09:20:00 PM UTC"