JavaScript Set Date Methods
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:
Example:
html
< body style = "text-align:center;" >
< h1 >GeeksForGeeks</ h1 >
< h2 >JavaScript setSeconds()</ h2 >
< p id = "GFG" ></ p >
< 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 >
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 >
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 >
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 >
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 >
var d = new Date();
d.setMonth(5);
document.getElementById("GFG").innerHTML = d;
</ script >
</ body >
|
Output:
Last Updated :
30 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...