timestamper.online

How to Work with Unix Timestamps in JavaScript

A complete guide to handling Unix timestamps in JavaScript, focusing on the key differences like millisecond precision.

🚀 Quick Converter
Need to convert a timestamp right now? Use the tool below.
🚀 Try It Now - Convert Instantly
Paste your Unix timestamp below to see instant conversion

Result will appear here...

🔥 Popular: 1,200+ developers used this tool in the last 24 hours

The Millisecond Difference

The most important thing to remember when working with JavaScript is that its native `Date` object operates with **milliseconds** since the epoch, not seconds. This is a common source of bugs when interacting with backend systems (like Python, PHP, or SQL databases) that often use second-precision timestamps.

1. Get the Current Unix Timestamp

Use `Date.now()` to get the current timestamp in milliseconds. To get it in seconds, you must divide by 1000 and remove the decimal part.

javascript
// Get current timestamp in MILLISECONDS
const timestampInMs = Date.now();
console.log(timestampInMs); // Example: 1678886400123

// Get current timestamp in SECONDS
const timestampInSeconds = Math.floor(Date.now() / 1000);
console.log(timestampInSeconds); // Example: 1678886400

2. Convert a Unix Timestamp to a Date Object

To convert a timestamp into a `Date` object, pass the timestamp to the `new Date()` constructor. Remember to use milliseconds!

javascript
const timestampInMs = 1678886400123;

// Create a Date object
const dateObject = new Date(timestampInMs);
console.log(dateObject); // Shows the full date object

// To see it in a standard format, use .toISOString()
console.log(dateObject.toISOString()); // "2023-03-15T13:20:00.123Z"

Handling Timestamps in Seconds

If you receive a timestamp in seconds from a backend API, you must convert it to milliseconds before passing it to the `Date` constructor.

javascript
const timestampInSeconds = 1678886400;

// Multiply by 1000 to convert to milliseconds
const dateObject = new Date(timestampInSeconds * 1000);

console.log(dateObject.toISOString()); // "2023-03-15T13:20:00.000Z"

3. Convert a Date Object to a Unix Timestamp

To get the millisecond timestamp from a `Date` object, use the `.getTime()` method.

javascript
// Create a specific date object
const myDate = new Date('2024-10-12T10:30:00Z');

// Get the timestamp in milliseconds
const timestampInMs = myDate.getTime();
console.log(timestampInMs); // 1728712200000

// To get the timestamp in seconds for a backend
const timestampInSeconds = Math.floor(myDate.getTime() / 1000);
console.log(timestampInSeconds); // 1728712200

4. Formatting Dates for Display

Once you have a `Date` object, you have several options for formatting it into a human-readable string.

javascript
const date = new Date(1678886400123);

// 1. ISO 8601 format (UTC)
console.log(date.toISOString()); // "2023-03-15T13:20:00.123Z"

// 2. UTC String
console.log(date.toUTCString()); // "Wed, 15 Mar 2023 13:20:00 GMT"

// 3. Localized String (respects user's browser locale and timezone)
console.log(date.toLocaleString()); // "3/15/2023, 9:20:00 AM" (Example output in America/New_York)

// 4. Advanced Formatting with Intl.DateTimeFormat (Recommended)
const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' };
const formatter = new Intl.DateTimeFormat('en-US', options);
console.log(formatter.format(date)); // "March 15, 2023, 09:20 AM"