JavaScript provides excellent support for Unix timestamps through the built-in Date object. This guide covers all the essential operations you need to handle epoch time in JavaScript, including getting the current timestamp, converting dates to timestamps, and converting timestamps back to readable dates.
How to get the current epoch time
Math.floor(new Date().getTime()/1000.0)The getTime method returns the time in milliseconds.
Convert from human-readable date to epoch
Use the JavaScript Date objectConvert from epoch to human-readable date
Use the JavaScript Date objectAdditional JavaScript Examples
Vanilla JavaScript (Browser & Node.js)
// Get current timestamp in milliseconds
const timestampMs = Date.now();
console.log(timestampMs); // e.g., 1705929600000
// Get current timestamp in seconds
const timestampSec = Math.floor(Date.now() / 1000);
console.log(timestampSec); // e.g., 1705929600
// Convert timestamp to Date
const date = new Date(1705929600000);
console.log(date.toISOString()); // "2024-01-22T12:00:00.000Z"
// Convert Date to timestamp
const timestamp = new Date('2024-01-22T12:00:00Z').getTime();
console.log(timestamp);Vanilla JavaScript Date API works in both browser and Node.js environments.
Node.js Specific
// Node.js: Using process.hrtime.bigint() for high precision
const { hrtime } = require('process');
const start = hrtime.bigint();
// ... do work ...
const end = hrtime.bigint();
const nanoseconds = end - start;
const milliseconds = Number(nanoseconds) / 1000000;
// Node.js: Using performance.now() (available in Node.js 16+)
const { performance } = require('perf_hooks');
const perfNow = performance.now(); // High-resolution time in milliseconds
// Node.js: Using Date.now() (same as browser)
const timestamp = Date.now();
console.log(timestamp);Node.js provides additional timing functions for high-precision measurements.
Using Day.js (Modern Alternative to Moment.js)
// Install: npm install dayjs
import dayjs from 'dayjs';
// Get current timestamp
const now = dayjs();
const timestampSeconds = now.unix(); // Unix timestamp in seconds
const timestampMillis = now.valueOf(); // Unix timestamp in milliseconds
// Convert timestamp to Day.js object
const date = dayjs.unix(1609459200); // From seconds
const date2 = dayjs(1609459200000); // From milliseconds
// Format date
const formatted = date.format('YYYY-MM-DD HH:mm:ss');
console.log(formatted); // "2021-01-01 00:00:00"
// Convert date string to timestamp
const timestamp = dayjs('2024-01-01 12:00:00').unix();
console.log(timestamp);
// Timezone support (requires timezone plugin)
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
const utcDate = dayjs.unix(1609459200).utc();
const nyDate = dayjs.unix(1609459200).tz('America/New_York');Day.js is a modern, lightweight (2KB) alternative to Moment.js with a similar API. It's immutable and supports plugins for additional functionality.
Best Practices
- Always use
Math.floor()when converting to seconds to avoid decimals - Be aware of timezone differences when working with timestamps
- Use
toISOString()for consistent UTC formatting - Consider using libraries like
date-fnsormoment.jsfor complex date operations
About JavaScript Unix Timestamps
JavaScript provides excellent support for Unix timestamps through the built-in Date object. Whether you're working in the browser or Node.js, JavaScript makes it easy to get the current timestamp, convert between timestamps and dates, and perform various date calculations. This guide covers all essential operations including vanilla JavaScript, modern libraries like Day.js, and Node.js-specific timing functions.
Related guides: Check out our guides for other programming languages: Python, Java, PHP, and more. For timestamp conversion tools, visit our Tools page.
Frequently Asked Questions
How do I get the current Unix timestamp in JavaScript?
Use Date.now() to get the current timestamp in milliseconds, or Math.floor(Date.now() / 1000) to get it in seconds. The Date object provides excellent support for Unix timestamps in JavaScript.
How do I convert a Unix timestamp to a date in JavaScript?
Create a new Date object using the timestamp: new Date(timestamp). For timestamps in seconds, multiply by 1000 first: new Date(timestamp * 1000). You can then format it using methods like toISOString() or toLocaleString().
What is the difference between Date.now() and new Date().getTime()?
Date.now() is a static method that returns the current timestamp directly, while new Date().getTime() creates a Date object first then gets its timestamp. Date.now() is more efficient and is the recommended approach.