How to get the current year using JavaScript ?
Last Updated :
11 Nov, 2021
In this article, we will know how to get the current year using the build-in method in Javascript. The purpose is to get the current year by using JavaScript getFullYear() method that will return the full year in 4-digit format for some specified date. This method is used to fetch the year from a given Date object.
Syntax:
DateObject.getFullYear();
Parameter: This function does not accept any parameters.
Return Values: It returns the year for the given date.
Example: This example describes getting the current year. Generally, it is used to validate the input year in the 4-digit format on the website.
HTML
<!DOCTYPE html>
< html >
< body style = "text-align:center;" >
< h1 >GeeksforGeeks</ h1 >
< h2 >
How to get the current
year in JavaScript?
</ h2 >
< button onclick = "geeks()" > click me </ button >
< h4 id = "myID" ></ h4 >
< script >
function geeks() {
// Creating Date Object
var dateobj = new Date();
// Year from above object
// is being fetched using getFullYear()
var dateObject = dateobj.getFullYear();
// Printing current year
document.getElementById("myID").innerHTML =
"The Current Year is : " + dateObject;
}
</ script >
</ body >
</ html >
|
Output:
getting the current year
Share your thoughts in the comments
Please Login to comment...