JS Date Set
In this page:
date.setFullYear(year);
date.setMonth(month);
date.setDate(day);
date.setHours(hours);
Setting the Year, Month, and Day
setFullYear(year), setMonth(month), and setDate(day) each modify one specific part of an existing Date object -- with setMonth() using the same zero-indexed convention (0 for January) as its getMonth() counterpart, a detail easy to trip over.
उदाहरण: Setting the Year, Month, and Day
const date = new Date(2024, 0, 1);
date.setFullYear(2025);
date.setMonth(5); // 0-indexed, 5 = June
date.setDate(15);
console.log(date.toDateString());
Setting Time Components
setHours(), setMinutes(), setSeconds(), and setMilliseconds() modify a Date's time-of-day portion, exactly the way the year/month/day setters modify its calendar portion -- useful for adjusting just the time while keeping the same calendar date.
उदाहरण: Setting Time Components
// Declare the constant `date` as a new `Date` instance
// Declare the constant `date` as a new `Date` instance
const date = new Date();
// Call `date.setHours(10)`
// Call `date.setHours(10)`
date.setHours(10);
// Call `date.setMinutes(30)`
// Call `date.setMinutes(30)`
date.setMinutes(30);
// Call `date.setSeconds(0)`
// Call `date.setSeconds(0)`
date.setSeconds(0);
// Print `date.toTimeString()` to the console
// Print `date.toTimeString()` to the console
console.log(date.toTimeString());
Computing Relative Dates
A clever pattern combines a getter and setter of the same unit: date.setDate(date.getDate() + 7) computes 'one week from now' by reading the current day and adding 7, letting JavaScript's Date object handle any month or year rollover automatically.
उदाहरण: Computing Relative Dates
const date = new Date(2024, 0, 28);
date.setDate(date.getDate() + 7); // one week from now, handles rollover
console.log(date.toDateString());
Setting a Date to the Start or End of a Period
Combining setters lets you snap a date to a meaningful boundary -- setting the day to 1 to get the start of the month, or using setMonth(date.getMonth() + 1, 0) to get the last day of the current month, both common needs when building calendar or reporting features.
उदाहरण: Setting a Date to the Start or End of a Period
const date = new Date(2024, 5, 15);
date.setDate(1); // start of the month
console.log(date.toDateString());
const lastDay = new Date(2024, 5, 15);
lastDay.setMonth(lastDay.getMonth() + 1, 0); // last day of the month
console.log(lastDay.toDateString());
Set Methods Return a Timestamp, Not a Date
Every set method returns the Date's new numeric timestamp (milliseconds since 1970) as its return value, not a new Date object -- the original Date instance itself is what got modified in place, and that same instance should be used going forward, not the setter's return value.
उदाहरण: Set Methods Return a Timestamp, Not a Date
const date = new Date(2024, 0, 1);
const result = date.setFullYear(2025); // returns a timestamp, not a Date
console.log(typeof result, result);
console.log(date.toDateString()); // use the original instance
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic