How to Get Current Year in JavaScript
![How to Get Current Year in JavaScript](https://javascripts.com/wp-content/uploads/2023/06/k2jcdgcz4njgmt6vwndb-1200x675.png)
JavaScript, a powerful programming language, offers a myriad of functions to make our tasks easier. One such function pertains to retrieving the current year, a frequently encountered requirement in web development.
You might wonder, why do we need to fetch the current year? Well, imagine updating the copyright year manually on your website every 365 days – quite a tedious job, right? Luckily, JavaScript provides us with an easy solution to automate this process, and we’re going to delve into how to use it.
In this post, we will be looking at the following 3 ways to get current year in JavaScript:
- By Date() object constructor
- By getFullYear() method
- By toISOString() method
Let’s explore each…
#1 – By Date() object constructor
The JavaScript Date object provides methods to work with dates and times. To get the current year, we can create a new Date object and use the getFullYear() method. This method returns the four-digit year of the specified date according to local time. Here is an example:
let currentDate = new Date();
let currentYear = currentDate.getFullYear();
console.log(currentYear);
How it works
The Date object constructor creates a new Date object, initialized to the current date and time. The getFullYear() method returns the full year of the date (a four-digit number for 4-digit years). Here’s a breakdown of the process:
- Step 1: We create a new Date object instance by calling
new Date()
. This object represents the current date and time. - Step 2: We use the
getFullYear()
method on the Date object. This method extracts the full year (four digits for four-digit years) from the Date object and assigns it to the variablecurrentYear
. - Step 3: We log the value of
currentYear
to the console to see the current year.
#2 – By getFullYear() method
The getFullYear() method in JavaScript is used to get the year of a specified date according to local time. For example, if you want to get the current year, you can use this method with the Date object.
var today = new Date();
var year = today.getFullYear();
console.log(year);
How it works
The getFullYear() method fetches the full year from a Date object in four digits (YYYY format). In our code, it’s used to get the current year from today’s date.
- First, we create a new Date object and assign it to the variable ‘today’. The Date object is instantiated with the current date and time.
- Then, we use the getFullYear() method with the ‘today’ object to fetch the current year. This method returns the year in four digits.
- Finally, we use console.log() to print the year. If you execute this code in your browser’s console, you’ll see the current year printed out.
#3 – By toISOString() method
The toISOString() method in JavaScript can help you extract the current year from a date object. This method returns a string in simplified extended ISO format (YYYY-MM-DDTHH:mm:ss.sssZ), and from this string, you can extract the year part.
const currentYear = new Date().toISOString().split('-')[0];
How it works
The JavaScript’s toISOString() method is used to convert the current date into a string format following the ISO standard (YYYY-MM-DDTHH:mm:ss.sssZ). By splitting this string at the character ‘-‘, an array is generated where the first element corresponds to the year. The code essentially creates a new Date object (which is set by default to the current date and time), converts it into a string, then splits it to get the year.
- The
new Date()
generates a new Date object with the current date and time. - The
.toISOString()
method is called on the Date object, which converts the date and time into a string that follows the ISO standard. - The
.split('-')
method is called on the string, which splits it into an array of substrings every time it encounters a ‘-‘ character. The resulting array has the structure [YYYY, MM, DDTHH:mm:ss.sssZ]. - From this array, we pick the first element (at the 0 index), which corresponds to the year.
Related:
- How to Format JavaScript Datetime
- How to Access JavaScript Text Content
- How to Check if an Array is Empty in JavaScript
In conclusion, JavaScript’s built-in ‘Date’ object simplifies the process of getting the current year. It’s a handy tool that not only provides the current year but also a myriad of other date and time information.
Remember, consistency in updating your JavaScript skills is the key to mastering the language. Keep practicing, and you’ll be able to utilize JavaScript’s capabilities to their fullest in no time!