JavaScript date handling has a reputation for being confusing. Between the Date object, Unix timestamps, timezones, and formatting, there's a lot to know. This guide covers every operation you'll actually need.
Getting the current date and time
const now = new Date();
now.getTime() // Unix timestamp in milliseconds: 1700000000000
Math.floor(now.getTime() / 1000) // Unix timestamp in seconds: 1700000000
now.toISOString() // "2023-11-14T22:13:20.000Z"
now.toLocaleDateString() // "11/14/2023" (locale-dependent)
now.toLocaleString() // "11/14/2023, 10:13:20 PM"
now.toString() // "Tue Nov 14 2023 22:13:20 GMT+0000"
Creating a Date from a timestamp
// From Unix timestamp (seconds)
const date = new Date(1700000000 * 1000); // multiply by 1000
// From Unix timestamp (milliseconds)
const date = new Date(1700000000000);
// From ISO string
const date = new Date('2023-11-14T22:13:20Z');
// From date parts (CAREFUL: month is 0-indexed!)
const date = new Date(2023, 10, 14); // November 14 (month 10 = November)
Formatting dates
const date = new Date('2023-11-14T22:13:20Z');
// Intl.DateTimeFormat (recommended)
new Intl.DateTimeFormat('en-US').format(date)
// "11/14/2023"
new Intl.DateTimeFormat('en-GB').format(date)
// "14/11/2023"
new Intl.DateTimeFormat('en-US', {
year: 'numeric', month: 'long', day: 'numeric'
}).format(date)
// "November 14, 2023"
new Intl.DateTimeFormat('en-US', {
dateStyle: 'full', timeStyle: 'short'
}).format(date)
// "Tuesday, November 14, 2023 at 10:13 PM"
// Manual formatting
const pad = n => String(n).padStart(2, '0');
const d = new Date();
const formatted = d.getFullYear() + '-' + pad(d.getMonth()+1) + '-' + pad(d.getDate());
// "2023-11-14"
Date arithmetic
const now = new Date();
// Add 7 days
const nextWeek = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
// Add 1 month (careful with month-end edge cases)
const nextMonth = new Date(now);
nextMonth.setMonth(nextMonth.getMonth() + 1);
// Difference between two dates
const start = new Date('2023-01-01');
const end = new Date('2023-12-31');
const diffMs = end - start;
const diffDays = diffMs / (1000 * 60 * 60 * 24); // 364 days
// Is date in the past?
const isPast = date < new Date();
// Is date today?
const isToday = date.toDateString() === new Date().toDateString();
Timezone handling
// All Date objects are stored internally in UTC
const date = new Date('2023-11-14T22:13:20Z'); // Z = UTC
// Get UTC values
date.getUTCHours() // 22
date.getUTCDate() // 14
// Get local values (depends on system timezone)
date.getHours() // varies by timezone
date.getDate() // may differ from UTC date
// Format in specific timezone
new Intl.DateTimeFormat('en-US', {
timeZone: 'America/New_York',
dateStyle: 'short',
timeStyle: 'short'
}).format(date)
// Get timezone offset
date.getTimezoneOffset() // minutes offset from UTC (negative = east of UTC)
Common gotchas
- Months are 0-indexed: January = 0, December = 11
- new Date("2023-11-14") is UTC, new Date("11/14/2023") is local time — this trips everyone up
- Date comparison: Use
.getTime()for reliable comparison, not== - Invalid dates: Always check
isNaN(date.getTime())after parsing user input
Try it free — Unix Timestamp Converter
Convert timestamps to dates and back. Includes live current timestamp.