To get the current year in JavaScript, you can use the built-in Date
object and its getFullYear()
method. To get the current year in JavaScript, you can use the following syntax:
const now = new Date();
const currentYear = now.getFullYear();
JavaScript gets current year example
Simple example code
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to my web page!</h1>
<p>This page was last updated in <span id="current-year"></span>.</p>
<script>
const now = new Date();
const currentYear = now.getFullYear();
const currentYearElement = document.getElementById("current-year");
currentYearElement.textContent = currentYear;
</script>
</body>
</html>
Output:
![JavaScript get current year](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2023/05/JavaScript-get-current-year.jpg?resize=467%2C246&ssl=1)
When you load this page in a web browser, the text in the paragraph will be updated to show the current year.
Comment if you have any doubts or suggestions on this Js date object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version