JavaScript

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

JavaScript

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?


JS Data Set Methods Quiz

Q1: Which method sets the full year of a Date object?

A. setYear()
B. setFullYear()
C. changeYear()
D. updateYear()

Q2: What is the valid range for the setMonth() method?

A. 1–12
B. 0–11
C. 0–12
D. 1–11

Q3: What does date.setDate(25) do?

A. Sets the month to 25
B. Sets the day of the month to 25
C. Sets the hour to 25
D. Invalid method

Q4: Which method changes the time in milliseconds since 1970?

A. setDate()
B. setMilliseconds()
C. setTime()
D. setHour()

Q5: If you call setHours(14), what time is set?

A. 2 PM
B. 2 AM
C. 12 PM
D. 4 PM

Q6: Which method is used to set both the minutes and seconds?

A. setTime()
B. setClock()
C. setMinutes(30, 45)
D. setMinuteAndSecond()

Q7: What is the output of date.setMilliseconds(100)?

A. Sets seconds to 100
B. Adds 100 milliseconds to date
C. Replaces milliseconds part with 100
D. Sets time to 100 ms after epoch

Q8: If you use setMonth(12), how does JavaScript handle it?

A. Error
B. Sets December
C. Rolls over to next year and sets January
D. Resets to 0

Q9: Which method would you use to change seconds to 59?

A. setSeconds(59)
B. updateSeconds(59)
C. setSecond(59)
D. setSec(59)

Q10: What does setFullYear(2028, 5, 15) do?

A. Sets only the year
B. Sets year, month, and day
C. Error
D. Only updates year and month

Go Back Top