Code Example
// Current Unix timestamp (seconds)
const timestamp = Math.floor(Date.now() / 1000);
console.log(timestamp);
// Current timestamp (milliseconds)
const timestampMs = Date.now();
console.log(timestampMs);
// Timestamp to Date
const ts = 1704067200;
const date = new Date(ts * 1000); // multiply by 1000 for seconds
console.log(date.toISOString()); // '2024-01-01T00:00:00.000Z'
// Date to timestamp
const now = new Date();
const tsNow = Math.floor(now.getTime() / 1000);
// Parse date string to timestamp
const parsed = Date.parse('2025-01-27T12:00:00Z') / 1000;
// Format date
const options = { year: 'numeric', month: 'long', day: 'numeric' };
date.toLocaleDateString('en-US', options); // 'January 1, 2024'
// Add days
const future = new Date(date);
future.setDate(future.getDate() + 7);
// Difference between dates (in days)
const diff = Math.floor((date2 - date1) / (1000 * 60 * 60 * 24));
// Timezone offset (minutes)
const offset = new Date().getTimezoneOffset();
Key Points: JavaScript Date works in milliseconds. Divide/multiply by 1000 for Unix seconds. Use toISOString() for standard format.