You will be able to change year, month, day, hours, minutes, seconds, and milliseconds of a JavaScript Date object using set methods.
What it is
Date set methods are instance methods on Date objects that mutate the existing date instead of creating a new one. They let you update one or more parts of a date, such as setFullYear(), setMonth(), setDate(), setHours(), setMinutes(), setSeconds(), and setMilliseconds(). The mental model is: a Date object stores an internal timestamp, and a set method changes that timestamp by replacing or adjusting a calendar or time component.
Related terms: getters such as getFullYear() and getMonth(), setters, local time methods, UTC methods such as setUTCFullYear(), and time zone offset.
Why it matters
- Build deadlines, due dates, and expiration dates without manual arithmetic.
- Adjust dates for business rules, such as adding days or moving to the end of a month.
- Normalize user input from separate year, month, and day fields.
- Compare dates after setting them to midnight or end of day.
- Use UTC setters when storing timestamps consistently across time zones.
Syntax or steps
The smallest useful pattern is: create a Date, call a set method, then read the result.
const d = new Date();
d.setFullYear(2030);
console.log(d.toString());
Most setters accept one or more arguments. For example, setFullYear(year, month, day) can set year, month, and day together. Month is zero-based: January is 0, December is 11. Day is 1-based. Hours, minutes, seconds, and milliseconds use their normal clock ranges.
Example
const eventDate = new Date();
eventDate.setFullYear(2030, 5, 15); // June 15, 2030
eventDate.setHours(9, 30, 0, 0); // 09:30:00.000 local time
console.log(eventDate.getFullYear()); // 2030
console.log(eventDate.getMonth()); // 5
console.log(eventDate.getDate()); // 15
console.log(eventDate.getHours()); // 9
console.log(eventDate.getMinutes()); // 30
console.log(eventDate.toString());
Part by part:
new Date()creates a Date object set to the current local time.setFullYear(2030, 5, 15)changes the year to 2030, month to June, and day to 15.setHours(9, 30, 0, 0)sets hours, minutes, seconds, and milliseconds.- The getters confirm the changed parts.
Common mistakes
- Using month numbers 1 through 12. Fix: remember months are zero-based, so June is 5.
- Assuming setters return a new Date. Fix: they mutate the original object and return the new timestamp number.
- Forgetting local time versus UTC. Fix: use
setUTCFullYear()and related UTC methods when the date should be interpreted as UTC. - Setting only the month and expecting the day to stay safe. Fix: check the day, because setting a shorter month can roll the date into the next month.
When to use it
Use Date set methods when you already have a Date object and need to adjust it. Use the Date constructor when you want to create a new date from scratch or avoid mutating an existing object.
| Task | Use | Why |
|---|---|---|
| Change one part of an existing date | setFullYear(), setHours() |
Concise and mutates in place. |
| Create a date from components | new Date(year, month, day, ...) |
Clearer when no existing date is needed. |
| Keep original date unchanged | Copy first, then set | Set methods mutate the object. |
| Work in UTC | setUTCFullYear(), setUTCHours() |
Avoids local time zone surprises. |
Practice
Guided exercise: create a Date for today, then set it to the last day of the current month at 23:59:59.999.
const endOfMonth = new Date();
endOfMonth.setMonth(endOfMonth.getMonth() + 1, 0);
endOfMonth.setHours(23, 59, 59, 999);
console.log(endOfMonth.toString());
Challenge: set a date to exactly 7 days from now, but keep the same time of day. Hint: use setDate(getDate() + 7).
Quick check
Question: What does setMonth(11) mean?
Answer: It sets the month to December, because months are zero-based.
Summary
Date set methods are a practical way to mutate a JavaScript Date object by changing calendar and clock components. They are most useful when you already have a date and need to adjust it, but you must remember zero-based months, local time behavior, and mutation.