How to Get the Current Year in JavaScript

To get the current year in JavaScript, use the Date object and its getFullYear() method:

let currentYear = new Date().getFullYear();
console.log(currentYear); // e.g., 2024

This method provides a quick and easy way to retrieve the current year.

Obtaining the current year is a common task in JavaScript, whether you’re developing web applications, managing dates, or displaying current information to users. This article explores various methods to get the current year in JavaScript, providing detailed explanations and code examples for each approach.

Using the Date Object

The Date object in JavaScript is designed for working with dates and times. It’s the most straightforward way to get the current year.

Example 1: Using getFullYear()

let currentYear = new Date().getFullYear();
console.log(currentYear); // e.g., 2024

Explanation:

  • let currentYear = new Date().getFullYear();
    • new Date() creates a new Date object with the current date and time.
    • getFullYear() extracts the four-digit year from the Date object.
    • The current year is then stored in the variable currentYear.
  • console.log(currentYear);

Logs the value of currentYear, which is the current year (e.g., 2024).

Using toLocaleDateString()

The toLocaleDateString() method can be used to get the current year as part of a formatted date string.

Example 2: Using toLocaleDateString()

let options = { year: 'numeric' };
let currentYear = new Date().toLocaleDateString('en-US', options);
console.log(currentYear); // e.g., 2024

Explanation:

  • let options = { year: ‘numeric’ };
    Defines options to format the date string to include only the year.
  • let currentYear = new Date().toLocaleDateString(‘en-US’, options);
    • new Date() creates a new Date object with the current date and time.
    • toLocaleDateString(‘en-US’, options) formats the date according to the specified locale (‘en-US’) and options.
    • The formatted year is then stored in the variable currentYear.
  • console.log(currentYear);

Logs the value of currentYear, which is the current year (e.g., 2024).

Using Intl.DateTimeFormat

The Intl.DateTimeFormat object enables language-sensitive date and time formatting.

Example 3: Using Intl.DateTimeFormat

let currentYear = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(new Date());
console.log(currentYear); // e.g., 2024

Explanation:

  • let currentYear = new Intl.DateTimeFormat(‘en’, { year: ‘numeric’ }).format(new Date());
    • new Intl.DateTimeFormat(‘en’, { year: ‘numeric’ }) creates a formatter for the year.
    • format(new Date()) formats the current date according to the specified options.
    • The formatted year is then stored in the variable currentYear.
  • console.log(currentYear);

Logs the value of currentYear, which is the current year (e.g., 2024).

Using Moment.js Library

Moment.js is a popular library for handling dates in JavaScript. Though it’s being deprecated in favor of native methods, it is still widely used.

Example 4: Using Moment.js

First, include Moment.js in your project:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Then use Moment.js to get the current year:

let currentYear = moment().year();
console.log(currentYear); // e.g., 2024

Explanation:

  • let currentYear = moment().year();
    • moment() creates a new Moment.js object with the current date and time.
    • year() retrieves the year from the Moment.js object.
    • The current year is then stored in the variable currentYear.
  • console.log(currentYear);

Logs the value of currentYear, which is the current year (e.g., 2024).

Conclusion

Retrieving the current year in JavaScript can be done using various methods, each suited to different scenarios. Whether using the built-in Date object, formatting options, or external libraries like Moment.js, JavaScript provides robust tools for working with dates. Understanding these methods allows you to choose the most appropriate one for your specific needs, ensuring your code is efficient and effective.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top