-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
Modifying dates is an essential part of web development. Whether you are scheduling events, updating deadlines, calculating expiration dates, or manipulating timestamps, JavaScript provides a robust set of “set” methods in the Date object. These methods allow developers to change the year, month, day, hours, minutes, seconds, and milliseconds of a Date object. Understanding these methods is crucial for building applications that work with dynamic and calculated date values accurately.
In this tutorial, you will learn about JavaScript date set methods, how to use them effectively, practical examples, common mistakes, best practices, and real-world applications.
Set methods are important because they allow you to:
Adjust dates for events, reminders, and schedules
Calculate future or past dates dynamically
Correct user input or system-generated dates
Build date-based applications such as calendars, timers, and bookings
Combine with get methods to calculate durations or intervals
Without set methods, updating dates would require creating new Date objects from scratch, which is inefficient and prone to errors.
JavaScript provides multiple set methods to modify various components of a Date object.
Updates the year of a date.
let today = new Date();
today.setFullYear(2026);
console.log(today);
You can also provide optional month and day arguments:
today.setFullYear(2026, 5, 15); // Sets year to 2026, month to June (5), day to 15
console.log(today);
Updates the month of a date (0-11). January is 0, December is 11.
let event = new Date();
event.setMonth(11); // December
console.log(event);
Updates the day of the month (1-31).
let reminder = new Date();
reminder.setDate(25);
console.log(reminder);
Sets the hour of the date (0-23).
let meeting = new Date();
meeting.setHours(14);
console.log(meeting);
Updates the minutes (0-59).
let timer = new Date();
timer.setMinutes(30);
console.log(timer);
Updates the seconds (0-59).
let countdown = new Date();
countdown.setSeconds(45);
console.log(countdown);
Updates the milliseconds (0-999).
let startTime = new Date();
startTime.setMilliseconds(500);
console.log(startTime);
Sets the date based on milliseconds since January 1, 1970 (epoch time).
let customTime = new Date();
customTime.setTime(1672531200000);
console.log(customTime);
This is particularly useful for performing calculations or setting a date based on stored timestamp values.
let eventDate = new Date();
eventDate.setFullYear(2025);
eventDate.setMonth(11); // December
eventDate.setDate(31);
eventDate.setHours(18);
eventDate.setMinutes(30);
console.log(`Event scheduled for: ${eventDate.toString()}`);
let reminder = new Date();
reminder.setDate(reminder.getDate() + 7);
console.log(`Reminder set for: ${reminder.toDateString()}`);
let localTime = new Date();
let utcTime = new Date();
utcTime.setHours(localTime.getHours() - localTime.getTimezoneOffset() / 60);
console.log(`UTC Time: ${utcTime.toISOString()}`);
let subscription = new Date();
subscription.setMonth(subscription.getMonth() + 1);
console.log(`Subscription expires on: ${subscription.toDateString()}`);
let countdown = new Date();
countdown.setHours(countdown.getHours() + 2); // 2 hours later
countdown.setMinutes(countdown.getMinutes() + 15); // add 15 minutes
console.log(`Countdown ends at: ${countdown.toLocaleTimeString()}`);
let startReminder = new Date();
for (let i = 1; i <= 4; i++) {
let nextReminder = new Date(startReminder);
nextReminder.setDate(startReminder.getDate() + i * 7);
console.log(`Week ${i} reminder: ${nextReminder.toDateString()}`);
}
let timestamp = 1672531200000;
let specificDate = new Date();
specificDate.setTime(timestamp);
console.log(`Date from timestamp: ${specificDate.toString()}`);
Forgetting that months are zero-based when using setMonth()
Using setDate() when intending to change the day of the week
Ignoring daylight saving time changes when manipulating hours
Performing arithmetic on Date objects without converting to milliseconds
Overwriting dates without copying the original object when needed
Always validate date inputs before setting values
Use setTime() when working with timestamps for precise calculations
Combine get and set methods for dynamic updates
Keep calculations readable and clearly comment on changes
Test for edge cases such as month-end, leap years, and daylight saving time
Set methods are widely used in:
Scheduling and calendar applications
Subscription and membership expiration updates
Event countdowns and reminders
Time-based notifications and alerts
Calculating deadlines for tasks, invoices, or assignments
Synchronizing server and client times
JavaScript’s date set methods provide powerful tools for modifying and updating Date objects. Whether you need to change the year, month, day, hours, minutes, seconds, or milliseconds, these methods allow precise control over date and time values. By combining get and set methods, you can calculate dynamic dates, schedule events, and manipulate timestamps effectively. Following best practices and avoiding common mistakes ensures that your web applications handle date logic accurately and reliably.
Q1. How do you set the year of a Date object to 2035?
Q2. Which method is used to change the month to December?
Q3. How do you update the day of the month to the 10th?
Q4. Which method sets the hour to 3 PM (15 in 24-hour format)?
Q5. How can you set the minutes of a Date object to 45?
Q6. Which method would you use to change the time to a specific millisecond timestamp?add_tmcq/234
Q7. How do you modify a date to have 30 seconds?
Q8. What does setTime() accept as a parameter?
Q9. If you set setMonth(12), what will happen?
Q10. How do you set both hours and minutes at once to 10:20?
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
