JavaScript

coding learning websites codepractice

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 Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Data Set Methods


📘 JavaScript Date Set Methods – Modifying Date and Time Values

The Date Set methods in JavaScript are used to change or update individual components of a date object, such as the year, month, day, hour, etc.

These methods mutate the original date object.


🔹 Common Set Methods

Method Description Example
setFullYear(y) Sets the full year date.setFullYear(2030)
setMonth(m) Sets the month (0–11) date.setMonth(11) (Dec)
setDate(d) Sets day of the month (1–31) date.setDate(25)
setHours(h) Sets hour (0–23) date.setHours(15)
setMinutes(m) Sets minutes (0–59) date.setMinutes(30)
setSeconds(s) Sets seconds (0–59) date.setSeconds(45)
setMilliseconds(ms) Sets milliseconds (0–999) date.setMilliseconds(500)
setTime(ms) Sets time in milliseconds since Jan 1, 1970 date.setTime(1752196800000)

📝 All set methods return the new timestamp in milliseconds.


🔹 Example

let date = new Date();                   // Current date & time

date.setFullYear(2030);                 // Set year to 2030
date.setMonth(11);                      // December (0-based)
date.setDate(25);                       // 25th day
date.setHours(14);                      // 2 PM
date.setMinutes(30);                    // 30 minutes
date.setSeconds(15);                    // 15 seconds

console.log(date.toString());
// Example Output: "Wed Dec 25 2030 14:30:15 GMT+0530 (India Standard Time)"

Practice Questions

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?


JavaScript

online coding class codepractice

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 Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top