Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to display only the current year in JavaScript?
To display only the current year in JavaScript, use the getFullYear() method with the Date object. This method returns a four-digit year as a number.
Syntax
new Date().getFullYear()
Example: Display Current Year in HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Current Year Display</title>
</head>
<body>
<h1>The Current Year: <span id="currentYear"></span></h1>
<script>
document.getElementById("currentYear").innerHTML = new Date().getFullYear();
</script>
</body>
</html>
The Current Year: 2024
Console Output Example
const currentYear = new Date().getFullYear();
console.log("Current year:", currentYear);
console.log("Copyright © " + currentYear);
Current year: 2024 Copyright © 2024
Different Ways to Get Current Year
// Method 1: Direct call
console.log(new Date().getFullYear());
// Method 2: Store in variable
const year = new Date().getFullYear();
console.log(year);
// Method 3: Template literal
console.log(`Year: ${new Date().getFullYear()}`);
2024 2024 Year: 2024
Common Use Cases
The getFullYear() method is commonly used for:
- Copyright notices in website footers
- Age calculations
- Creating year-based file names
- Form validation for birth years
Key Points
-
getFullYear()returns a 4-digit number (e.g., 2024) - Always returns the current year based on system time
- No parameters needed
- Works in both browser and Node.js environments
Conclusion
Use new Date().getFullYear() to get the current year as a four-digit number. This method is reliable and works across all JavaScript environments for displaying copyright years, age calculations, and other year-based operations.
Advertisements
