Date & Time in JavaScript
JavaScript's Date object handles dates and times — displaying timestamps, calculating durations, formatting for display, and working with timezones. Every web app deals with dates.
Date Essentials
- new Date() — Current date and time
- new Date(year, month, day) — Specific date. Month is 0-indexed! (0 = January)
- Date.now() — Current timestamp (milliseconds since Jan 1, 1970). Fastest for timestamps
- getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds()
- toLocaleDateString() / toLocaleTimeString() — Format for user's locale
- Date math — Subtract dates to get milliseconds difference
Date & Time Code
// Current date
const now = new Date();
console.log(now); // Full date string
console.log(Date.now()); // Timestamp (ms)
// Get parts
console.log("Year:", now.getFullYear());
console.log("Month:", now.getMonth() + 1); // +1 because 0-indexed!
console.log("Day:", now.getDate());
console.log("Hours:", now.getHours());
console.log("Day of week:", now.getDay()); // 0=Sunday, 1=Monday...
// Format for display
console.log(now.toLocaleDateString("en-IN")); // "19/3/2026"
console.log(now.toLocaleTimeString("en-IN")); // "3:54:50 pm"
console.log(now.toLocaleDateString("en-IN", {
weekday: "long", year: "numeric", month: "long", day: "numeric"
})); // "Thursday, 19 March 2026"
// Specific date
const birthday = new Date(2000, 0, 15); // Jan 15, 2000 (month 0 = Jan!)
console.log("Birthday:", birthday.toLocaleDateString());
// Date math
const start = new Date("2026-01-01");
const end = new Date("2026-12-31");
const diffMs = end - start;
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
console.log(`${diffDays} days between dates`); // 364
// Time ago
function timeAgo(date) {
const seconds = Math.floor((Date.now() - date) / 1000);
if (seconds < 60) return `${seconds}s ago`;
if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
return `${Math.floor(seconds / 86400)}d ago`;
}
console.log(timeAgo(new Date(Date.now() - 3600000))); // "1h ago"Tip
Tip
Use Intl.DateTimeFormat for locale-aware date formatting instead of manually building date strings. new Intl.DateTimeFormat('en-IN', { dateStyle: 'full' }).format(new Date()) gives properly formatted dates for any locale.
Intl.DateTimeFormat for display. toISOString for storage/API. Avoid libraries when Intl API suffices.
Common Mistake
Warning
Months in JavaScript Date are 0-indexed (0=January, 11=December). new Date(2024, 0, 1) is January 1st, not a bug. Always remember to subtract 1 when setting months and add 1 when displaying.
Practice Task
Note
Date utility: (1) Display the current date in 3 different formats. (2) Calculate the number of days between two dates. (3) Create a function that returns 'X minutes ago' / 'X hours ago' relative time strings.
Quick Quiz
Key Takeaways
- JavaScript's Date object handles dates and times — displaying timestamps, calculating durations, formatting for display, and working with timezones.
- new Date() — Current date and time
- new Date(year, month, day) — Specific date. Month is 0-indexed! (0 = January)
- Date.now() — Current timestamp (milliseconds since Jan 1, 1970). Fastest for timestamps