Js Date Set Methods in JavaScript
?? JavaScript Date Set Methods
JavaScript’s Date object provides set methods to modify parts of a date/time.
Common Date Set Methods
| Method | Description | Parameter |
|---|---|---|
setFullYear(year, [month], [date]) | Sets the full year, optionally month and day | year (e.g., 2025), optional month (0–11), optional day (1–31) |
setMonth(month, [date]) | Sets the month and optionally day | month (0–11), optional day (1–31) |
setDate(date) | Sets the day of the month | date (1–31) |
setHours(hours, [min], [sec], [ms]) | Sets hours and optionally minutes, seconds, milliseconds | hours (0–23), optional minutes, seconds, ms |
setMinutes(minutes, [sec], [ms]) | Sets minutes and optionally seconds, milliseconds | minutes (0–59), optional seconds, ms |
setSeconds(seconds, [ms]) | Sets seconds and optionally milliseconds | seconds (0–59), optional ms |
setMilliseconds(ms) | Sets milliseconds | ms (0–999) |
setTime(timestamp) | Sets the time in milliseconds since Unix Epoch | timestamp (number) |
Examples
const date = new Date('2025-06-02T10:00:00');date.setFullYear(2030);date.setMonth(0); // January (months are 0-based)date.setDate(15);date.setHours(9, 30); // 9:30 AMdate.setMinutes(45);date.setSeconds(20);date.setMilliseconds(500);console.log(date.toISOString()); // Output: "2030-01-15T09:45:20.500Z"Notes
If you set a value out of the expected range, the
Dateobject will auto-adjust (e.g.,setDate(32)moves to the next month).Use
setTime()to set the full timestamp at once.
Want me to show UTC set methods or examples of how to use these to manipulate dates?