JavaScript Date set methods modify specific date and time components of an existing Date instance. Set methods automatically handle rollover calculations (e.g., setting day to 32 increments the month).
flowchart LR
SetOp["date.setDate(32) in January"] --> AutoCalc["Automatic Overflow Rollover"] --> Result["Date set to February 1st"]
| Local Set Method | UTC Counterpart | Description |
|---|---|---|
setFullYear(year, [m], [d]) |
setUTCFullYear() |
Sets 4-digit calendar year. |
setMonth(month, [d]) |
setUTCMonth() |
Sets zero-indexed month (0-11). |
setDate(day) |
setUTCDate() |
Sets day of month (1-31). |
setHours(h, [m], [s]) |
setUTCHours() |
Sets hour of day (0-23). |
setTime(ms) |
N/A | Sets date directly using epoch milliseconds. |
// Demonstrating Date Set Methods and Automatic Rollover Arithmetic
const targetDate = new Date("2026-08-13T10:00:00");
// 1. Modifying Year and Month
targetDate.setFullYear(2027);
targetDate.setMonth(0); // Set to January 2027
console.log(`Updated Date: ${targetDate.toLocaleDateString()}`);
// 2. Rollover Arithmetic (Adding 30 Days to current date)
const futureDate = new Date();
console.log(`Current Date: ${futureDate.toDateString()}`);
futureDate.setDate(futureDate.getDate() + 30); // Add 30 days
console.log(`Date 30 Days Later: ${futureDate.toDateString()}`);
// 3. Setting Time to Beginning of Day (00:00:00.000)
const startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);
console.log(`Start of Day Timestamp: ${startOfDay.toISOString()}`);
new Date(original)) if you need to preserve the original timestamp.setDate(getDate() + N) for simple date math instead of manually performing complex calendar calculations.setHours(23, 59, 59, 999).Write a function getLastDayOfMonth(year, month) that uses setDate(0) of the following month to return the last calendar day of the given month.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.