There are various methods to set the date in JavaScript. The data values can be set like years, months, days, hours, minutes, seconds, and 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.
Example:Â
html
< body style = "text-align:center;" > Â Â Â Â Â Â < h1 >GeeksForGeeks</ h1 > Â Â Â Â Â Â Â Â Â Â Â Â Â Â < h2 >JavaScript setSeconds()</ h2 > Â Â Â Â Â Â < p id = "GFG" ></ p > Â Â Â Â Â Â Â Â Â Â <!-- Script to use setSeconds method --> Â Â Â Â < script > Â Â Â Â Â Â Â Â var d = new Date(); Â Â Â Â Â Â Â Â d.setSeconds(22); Â Â Â Â Â Â Â Â document.getElementById("GFG").innerHTML = d; Â Â Â Â </ script > </ body > |
Output:
Â
The setMinutes() Method: The setMinutes() method sets the minutes of a date object (0-59).Â
Example:Â
html
< body style = "text-align:center;" > Â Â Â Â Â Â < h1 >GeeksForGeeks</ h1 > Â Â Â Â Â Â Â Â Â Â Â Â Â Â < h2 >JavaScript setMinutes()</ h2 > Â Â Â Â Â Â < p id = "GFG" ></ p > Â Â Â Â Â Â Â Â Â Â <!-- Script to use setMinutes method --> Â Â Â Â < script > Â Â Â Â Â Â Â Â var d = new Date(); Â Â Â Â Â Â Â Â d.setMinutes(2); Â Â Â Â Â Â Â Â document.getElementById("GFG").innerHTML = d; Â Â Â Â </ script > </ body > |
Output:Â
The setHours() Method: The setHours() method sets the hours of a date object (0-23).Â
Example:Â
html
< body style = "text-align:center;" > Â Â Â Â Â Â < h1 >GeeksForGeeks</ h1 > Â Â Â Â Â Â Â Â Â Â Â Â Â Â < h2 >JavaScript setHours()</ h2 > Â Â Â Â Â Â < p id = "GFG" ></ p > Â Â Â Â Â Â Â Â Â Â <!-- Script to use setHours() method --> Â Â Â Â < script > Â Â Â Â Â Â Â Â var d = new Date(); Â Â Â Â Â Â Â Â d.setHours(2); Â Â Â Â Â Â Â Â document.getElementById("GFG").innerHTML = d; Â Â Â Â </ script > </ body > |
Output:Â
The setDate() Method: The setDate() method in JavaScript is used to set the day of a date object (1-31).Â
Example:Â
html
< body style = "text-align:center;" > Â Â Â Â Â Â < h1 >GeeksForGeeks</ h1 > Â Â Â Â Â Â Â Â Â Â Â Â Â Â < h2 >JavaScript setDate()</ h2 > Â Â Â Â Â Â < p id = "GFG" ></ p > Â Â Â Â Â Â Â Â Â Â <!-- Script to use setDate method --> Â Â Â Â < script > Â Â Â Â Â Â Â Â var d = new Date(); Â Â Â Â Â Â Â Â d.setDate(5); Â Â Â Â Â Â Â Â document.getElementById("GFG").innerHTML = d; Â Â Â Â </ script > </ body > |
Output:Â
The setFullYear() Method: The setFullYear() method in JavaScript is used to set the year of a date object.Â
Example:Â
html
< body style = "text-align:center;" > Â Â Â Â Â Â < h1 >GeeksForGeeks</ h1 > Â Â Â Â Â Â Â Â Â Â Â Â Â Â < h2 >JavaScript setFullYear()</ h2 > Â Â Â Â Â Â < p id = "GFG" ></ p > Â Â Â Â Â Â Â Â Â Â <!-- Script to use setFullYear method --> Â Â Â Â < script > Â Â Â Â Â Â Â Â var d = new Date(); Â Â Â Â Â Â Â Â d.setFullYear(2020); Â Â Â Â Â Â Â Â document.getElementById("GFG").innerHTML = d; Â Â Â Â </ script > </ body > |
Output:Â
The setMonth() Method: The setMonth() method in JavaScript is used to set the month of a date object (0-11).Â
Example:Â
html
< body style = "text-align:center;" > Â Â Â Â Â Â < h1 >GeeksForGeeks</ h1 > Â Â Â Â Â Â Â Â Â Â Â Â Â Â < h2 >JavaScript setMonth()</ h2 > Â Â Â Â Â Â < p id = "GFG" ></ p > Â Â Â Â Â Â Â Â Â Â <!-- Script to use setMonth method --> Â Â Â Â < script > Â Â Â Â Â Â Â Â var d = new Date(); Â Â Â Â Â Â Â Â d.setMonth(5); Â Â Â Â Â Â Â Â document.getElementById("GFG").innerHTML = d; Â Â Â Â </ script > </ body > |
Output:Â