Syntax
new Date()
date.getFullYear()
date.getMonth()Examples
Creating Dates
Different ways to construct a Date object.
const now = new Date(); // current date and time
const specific = new Date(2024, 5, 15); // June 15, 2024 - month is 0-indexed!
const fromString = new Date("2024-06-15");
const fromTimestamp = new Date(1718409600000); // milliseconds since Jan 1, 1970
console.log(specific); // June 15 2024Getter Methods
Extracting individual components from a Date object.
const date = new Date(2024, 5, 15); // June 15, 2024
console.log(date.getFullYear()); // 2024
console.log(date.getMonth()); // 5 - remember, 0-indexed! (0=Jan, 5=Jun)
console.log(date.getDate()); // 15 - day of the month
console.log(date.getDay()); // day of the week (0=Sunday, 6=Saturday)
console.log(date.getHours()); // hour (0-23)Date.now() and Timestamps
Getting the current timestamp for calculations, like measuring elapsed time.
const start = Date.now(); // current timestamp in milliseconds
// ... some operation happens ...
const end = Date.now();
console.log(`Took ${end - start}ms`);
// Comparing two dates by their timestamps
const date1 = new Date(2024, 0, 1);
const date2 = new Date(2024, 5, 1);
console.log(date2.getTime() > date1.getTime()); // trueBest practices
- Remember getMonth() is zero-indexed (January is 0, December is 11) - a very common source of off-by-one bugs
- Use Date.now() rather than new Date().getTime() when you just need the current timestamp for comparisons or performance measurement
- Prefer ISO 8601 format ("2024-06-15") when creating dates from strings, since other formats can be parsed inconsistently across browsers
- For serious date manipulation (time zones, formatting, arithmetic), consider a dedicated library or the newer Temporal API rather than hand-rolling logic with the legacy Date object
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Date: Setting and Formatting
Setter methods like setFullYear() and setDate() modify a Date object in place. Formatting methods convert a Date into a readable string: toDateString() and toTimeString() give simple readable formats, toISOString() gives the standardized ISO 8601 format (useful for APIs), and toLocaleDateString() formats according to a locale's conventions.Temporal API
Temporal is the modern, immutable replacement for the long-criticized Date object, reaching TC39 Stage 4 and becoming part of the ECMAScript 2026 specification. It fixes Date's biggest pain points: no built-in time zone support, confusing zero-indexed months, and mutable objects that are easy to accidentally modify. Temporal provides distinct types for different use cases - PlainDate, PlainTime, ZonedDateTime, Duration, and more.Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.
Setter methods like setFullYear() and setDate() modify a Date object in place. Formatting methods convert a Date into a readable string: toDateString() and toTimeString() give simple readable formats, toISOString() gives the standardized ISO 8601 format (useful for APIs), and toLocaleDateString() formats according to a locale's conventions.Temporal API
Temporal is the modern, immutable replacement for the long-criticized Date object, reaching TC39 Stage 4 and becoming part of the ECMAScript 2026 specification. It fixes Date's biggest pain points: no built-in time zone support, confusing zero-indexed months, and mutable objects that are easy to accidentally modify. Temporal provides distinct types for different use cases - PlainDate, PlainTime, ZonedDateTime, Duration, and more.Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.