There are various methods to set the date in JavaScript. The data values can be set like years, months, days, hours, minutes, seconds, milliseconds for a Date Object.
Method:
- setDate(): It is used to set the day as a number (1-31).
- setFullYear(): It is used to set the year (optionally month and day).
- setHours(): It is used to set the hour (0-23).
- setMilliseconds(): It is used to set the milliseconds (0-999).
- setMinutes(): It is used to set the minutes (0-59).
- setMonth(): It is used to set the month (0-11).
- setSeconds(): It is used to set the seconds (0-59).
- setTime(): It is used to set the time.
- The setSeconds() Method: The setSeconds() method sets the seconds of a date object (0-59).
Example:
<!DOCTYPE html><html><head><title>JavaScript setSeconds() method</title></head><bodystyle="text-align:center;"><h1>GeeksForGeeks</h1><h2>JavaScript setSeconds()</h2><pid="GFG"></p><!-- Script to use setSeconds method --><script>var d = new Date();d.setSeconds(22);document.getElementById("GFG").innerHTML = d;</script></body></html>chevron_rightfilter_noneOutput:

- The setMinutes() Method: The setMinutes() method sets the minutes of a date object (0-59).
Example:
<!DOCTYPE html><html><head><title>JavaScript setMinutes() method</title></head><bodystyle="text-align:center;"><h1>GeeksForGeeks</h1><h2>JavaScript setMinutes()</h2><pid="GFG"></p><!-- Script to use setMinutes method --><script>var d = new Date();d.setMinutes(2);document.getElementById("GFG").innerHTML = d;</script></body></html>chevron_rightfilter_noneOutput:

- The setHours() Method: The setHours() method sets the hours of a date object (0-23).
Example:
<!DOCTYPE html><html><head><title>JavaScript setHours() method</title></head><bodystyle="text-align:center;"><h1>GeeksForGeeks</h1><h2>JavaScript setHours()</h2><pid="GFG"></p><!-- Script to use setHours() method --><script>var d = new Date();d.setHours(2);document.getElementById("GFG").innerHTML = d;</script></body></html>chevron_rightfilter_noneOutput:

- The setDate() Method: The setDate() method in JavaScript is used to set the day of a date object (1-31).
Example:
<!DOCTYPE html><html><head><title>JavaScript setDate() method</title></head><bodystyle="text-align:center;"><h1>GeeksForGeeks</h1><h2>JavaScript setDate()</h2><pid="GFG"></p><!-- Script to use setDate method --><script>var d = new Date();d.setDate(5);document.getElementById("GFG").innerHTML = d;</script></body></html>chevron_rightfilter_noneOutput:

- The setFullYear() Method: The setFullYear() method in JavaScript is used to set the year of a date object.
Example:
<!DOCTYPE html><html><head><title>JavaScript setFullYear() method</title></head><bodystyle="text-align:center;"><h1>GeeksForGeeks</h1><h2>JavaScript setFullYear()</h2><pid="GFG"></p><!-- Script to use setFullYear method --><script>var d = new Date();d.setFullYear(2020);document.getElementById("GFG").innerHTML = d;</script></body></html>chevron_rightfilter_noneOutput:

- The setMonth() Method: The setMonth() method in JavaScript is used to set the month of a date object (0-11).
Example:
<!DOCTYPE html><html><head><title>JavaScript setMonth() method</title></head><bodystyle="text-align:center;"><h1>GeeksForGeeks</h1><h2>JavaScript setMonth()</h2><pid="GFG"></p><!-- Script to use setMonth method --><script>var d = new Date();d.setMonth(5);document.getElementById("GFG").innerHTML = d;</script></body></html>chevron_rightfilter_noneOutput:

Recommended Posts:
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- JavaScript | Get Date Methods
- How to check the input date is equal to today's date or not using JavaScript ?
- How to convert UTC date time into local date time using JavaScript ?
- JavaScript Basic Array Methods
- JavaScript | Object Methods
- JavaScript | Array Iteration Methods
- JavaScript methods to increment/decrement alphabetical letters
- How to get all the methods of an object using JavaScript ?
- Best-Known JavaScript Array Methods
- What is the difference between find() and filter() methods in JavaScript ?
- What is the difference between every() and some() methods in JavaScript ?
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- JavaScript | String Methods
- Chaining of Array Methods in JavaScript
- Static Methods in JavaScript
- Some newly introduced array methods in JavaScript ES6
- JavaScript | Adding minutes to Date object
- JavaScript Date toLocaleTimeString() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


