JavaScript Date Set Methods
Date set methods allow you to modify parts of an existing Date object. Each set method changes the date in place and returns the new timestamp in milliseconds.
setFullYear()
The setFullYear() method sets the year of a date. It can optionally set month and day as well.
const d = new Date();
console.log("Before:", d.toDateString());
// Set year only
d.setFullYear(2025);
console.log("After setFullYear(2025):", d.toDateString());
// Set year, month, day
d.setFullYear(2025, 11, 25);
console.log("After setFullYear(2025, 11, 25):", d.toDateString());setMonth() and setDate()
setMonth() sets the month (0-11) and optionally the day. setDate() sets the day of the month. Setting setDate(0) gives you the last day of the previous month.
const d = new Date("2024-06-15");
// Set month (0 = January)
d.setMonth(0);
console.log("January:", d.toDateString());
// Set date
d.setDate(28);
console.log("28th:", d.toDateString());
// Get last day of current month
const lastDay = new Date(2024, 2, 0);
console.log("Last day of Feb 2024:", lastDay.getDate());
// Add days by setting date beyond range
const d2 = new Date("2024-01-31");
d2.setDate(d2.getDate() + 1);
console.log("Jan 31 + 1 day:", d2.toDateString());setHours(), setMinutes(), setSeconds()
These methods set the time components of a date. Each accepts optional additional parameters for the smaller time units.
const d = new Date("2024-06-15T10:30:00");
console.log("Before:", d.toTimeString());
// Set hours
d.setHours(14);
console.log("After setHours(14):", d.toTimeString());
// Set minutes
d.setMinutes(45);
console.log("After setMinutes(45):", d.toTimeString());
// Set seconds
d.setSeconds(30);
console.log("After setSeconds(30):", d.toTimeString());
// Set hours, minutes, seconds at once
d.setHours(8, 0, 0, 0);
console.log("After setHours(8,0,0,0):", d.toTimeString());Comparing Dates
Dates can be compared using comparison operators or by comparing their timestamps with getTime(). Two Date objects are never equal with === even if they represent the same time.
const d1 = new Date("2024-06-15");
const d2 = new Date("2024-12-25");
const d3 = new Date("2024-06-15");
// Direct comparison works with < and >
console.log("d1 < d2:", d1 < d2);
console.log("d1 > d2:", d1 > d2);
// === does NOT work for equality
console.log("d1 === d3:", d1 === d3);
// Use getTime() for equality check
console.log("d1 equals d3:", d1.getTime() === d3.getTime());
// Check if a date is in the past
const past = new Date("2020-01-01");
console.log("Is past:", past < new Date());Practical Date Manipulation
By combining set methods with get methods, you can perform common date operations like adding days, months, or finding the start/end of a period.
// Add 7 days to a date
const d = new Date("2024-06-15");
d.setDate(d.getDate() + 7);
console.log("Plus 7 days:", d.toDateString());
// Add 3 months
const d2 = new Date("2024-06-15");
d2.setMonth(d2.getMonth() + 3);
console.log("Plus 3 months:", d2.toDateString());
// Get start of day
const startOfDay = new Date("2024-06-15T14:30:00");
startOfDay.setHours(0, 0, 0, 0);
console.log("Start of day:", startOfDay.toISOString());
// Get end of day
const endOfDay = new Date("2024-06-15T14:30:00");
endOfDay.setHours(23, 59, 59, 999);
console.log("End of day:", endOfDay.toISOString());| Method | Description |
|---|---|
| setFullYear(year, month, day) | Sets year, optionally month and day |
| setMonth(month, day) | Sets month (0-11), optionally day |
| setDate(day) | Sets day of month (1-31) |
| setHours(h, m, s, ms) | Sets hours (0-23), optionally m/s/ms |
| setMinutes(m, s, ms) | Sets minutes (0-59) |
| setSeconds(s, ms) | Sets seconds (0-59) |
| setMilliseconds(ms) | Sets milliseconds (0-999) |
| setTime(ms) | Sets date from milliseconds since epoch |