JavaScript
JavaScript Date Methods Cheatsheet
Quick reference for JavaScript Date methods including constructors, getters, setters, formatting, parsing, and timestamp operations.
Creating Dates
new Date(); // Current date/time
new Date(2024, 0, 15); // Jan 15, 2024 (month is 0-indexed)
new Date(2024, 0, 15, 10, 30, 0); // Jan 15, 2024 10:30:00
new Date("2024-01-15"); // Parse ISO string
new Date("2024-01-15T10:30:00Z"); // Parse ISO with time
new Date(1705312200000); // From timestamp (ms)
Static Methods
| Method | Returns | Description |
|---|
Date.now() | Number | Current timestamp (ms since epoch) |
Date.parse(str) | Number | Parse string to timestamp |
Date.UTC(y, m, d, ...) | Number | UTC timestamp from components |
Date.now(); // 1705312200000
Date.parse("2024-01-15"); // 1705276800000
Date.UTC(2024, 0, 15); // 1705276800000
Getters (Local Time)
| Method | Returns |
|---|
getFullYear() | Year (4 digits) |
getMonth() | Month (0-11) |
getDate() | Day of month (1-31) |
getDay() | Day of week (0=Sun, 6=Sat) |
getHours() | Hours (0-23) |
getMinutes() | Minutes (0-59) |
getSeconds() | Seconds (0-59) |
getMilliseconds() | Milliseconds (0-999) |
getTime() | Timestamp (ms since epoch) |
getTimezoneOffset() | UTC offset in minutes |
Getters (UTC)
| Method | Returns |
|---|
getUTCFullYear() | Year |
getUTCMonth() | Month (0-11) |
getUTCDate() | Day of month |
getUTCDay() | Day of week |
getUTCHours() | Hours |
getUTCMinutes() | Minutes |
getUTCSeconds() | Seconds |
getUTCMilliseconds() | Milliseconds |
Setters
| Method | Description |
|---|
setFullYear(y, m?, d?) | Set year (optionally month, day) |
setMonth(m, d?) | Set month (0-11) |
setDate(d) | Set day of month |
setHours(h, m?, s?, ms?) | Set hours |
setMinutes(m, s?, ms?) | Set minutes |
setSeconds(s, ms?) | Set seconds |
setMilliseconds(ms) | Set milliseconds |
setTime(ms) | Set from timestamp |
All setters have UTC equivalents: setUTCFullYear(), setUTCMonth(), etc.
| Method | Example Output |
|---|
toString() | "Mon Jan 15 2024 10:30:00 GMT+0000" |
toISOString() | "2024-01-15T10:30:00.000Z" |
toUTCString() | "Mon, 15 Jan 2024 10:30:00 GMT" |
toLocaleDateString() | "1/15/2024" (locale-dependent) |
toLocaleTimeString() | "10:30:00 AM" (locale-dependent) |
toLocaleString() | "1/15/2024, 10:30:00 AM" |
toDateString() | "Mon Jan 15 2024" |
toTimeString() | "10:30:00 GMT+0000" |
toJSON() | "2024-01-15T10:30:00.000Z" |
new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "long",
day: "numeric",
}).format(date); // "January 15, 2024"
date.toLocaleDateString("en-US", {
weekday: "short",
year: "numeric",
month: "short",
day: "numeric",
}); // "Mon, Jan 15, 2024"
| Option | Values |
|---|
year | "numeric", "2-digit" |
month | "numeric", "2-digit", "long", "short", "narrow" |
day | "numeric", "2-digit" |
weekday | "long", "short", "narrow" |
hour | "numeric", "2-digit" |
minute | "numeric", "2-digit" |
second | "numeric", "2-digit" |
timeZoneName | "long", "short" |
hour12 | true, false |
timeZone | "UTC", "America/New_York", etc. |
Comparison & Arithmetic
// Compare dates
date1 > date2;
date1.getTime() === date2.getTime();
// Difference in days
Math.round((date2 - date1) / (1000 * 60 * 60 * 24));
// Add days
const tomorrow = new Date(date);
tomorrow.setDate(tomorrow.getDate() + 1);
// Add months
const nextMonth = new Date(date);
nextMonth.setMonth(nextMonth.getMonth() + 1);
// Start of day
const startOfDay = new Date(date);
startOfDay.setHours(0, 0, 0, 0);
Common Recipes
// ISO date string (YYYY-MM-DD)
date.toISOString().split("T")[0];
// Relative time
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
rtf.format(-1, "day"); // "yesterday"
rtf.format(3, "hour"); // "in 3 hours"
// Check if valid date
!isNaN(new Date(input).getTime());
// Days in month
new Date(2024, 2, 0).getDate(); // 29 (Feb 2024, leap year)
// Clone a date
const clone = new Date(date.getTime());