Get the Last 2 Digits of a Year in JavaScript

avatar

Borislav Hadzhiev

Sun Oct 24 20212 min read

Get the Last 2 Digits of a Year #

To get the last 2 digits of a year:

  1. Convert the year to a string, using the toString() method.
  2. Call the slice() method on the year, passing it -2 as a parameter.
  3. The slice method will return a string containing the last 2 digits of the year.
index.js
// ๐Ÿ‘‡๏ธ Get last 2 digits of current year const last2 = new Date().getFullYear().toString().slice(-2); console.log(last2); // ๐Ÿ‘‰๏ธ has type of string const last2Num = Number(last2); // ๐Ÿ‘‡๏ธ Get last 2 digits of a Year const date = new Date('April 04, 2025 05:24:07'); const last2Again = date.getFullYear().toString().slice(-2); console.log(last2Again); // ๐Ÿ‘‰๏ธ '25'

We used the Date.getFullYear method to get the year of a given date. However, the method returns a number representation of the year, so we had to convert it to a string.

The last step is to use the String.slice method.

The only parameter we passed to the slice method is the start index - the index at which we start extraction of characters from the string.

Passing a negative index of -2 means - give me the last 2 characters of the string.

This is the same as passing string.length - 2 as the start index.

Indexes are zero-based in JavaScript, meaning the index of the first character in a string is 0 and the index of the last - string.length - 1.
index.js
const str = '2025'; console.log(str.slice(-2)); // ๐Ÿ‘‰๏ธ 25 console.log(str.slice(str.length - 2)); // ๐Ÿ‘‰๏ธ 25

In both examples, we tell the slice method to copy the last 2 characters of the string into a new string.

Note that the slice method returns a string, so if you need the last 2 digits of the year as a number, you have to convert the string using the Number object.

An alternative approach is to use the String.substring method.

To get the last 2 digits of a year:

  1. Convert the year to a string, using the toString method.
  2. Call the substring() method, passing it year.length - 2 as a parameter.
  3. The substring method will return a new string containing the last 2 digits of the year.
index.js
const currentYearStr = new Date().getFullYear().toString() const last2 = currentYearStr.substring(currentYearStr.length - 2); console.log(last2); // ๐Ÿ‘‰๏ธ has type of string const last2Num = Number(last2);

This example is very similar to the last one, however this time we used the substring method.

The substring method doesn't handle negative indexes like slice does, so we used the str.length - 2 approach.

You could also simply pass 2 as a parameter to the slice and substring methods, which would mean, start extraction at index 2.

index.js
const last2 = new Date().getFullYear().toString().substring(2); console.log(last2); // ๐Ÿ‘‰๏ธ has type of string const last2Num = Number(last2);

This code achieves the same goal as the last code snippet, so it's a matter of a personal preference.

There are some differences between the slice and substring methods. For example, if you pass a negative argument to the String.substring method, it treats it as if you passed 0, which is very confusing.

For other differences between String.substring and String.slice, check out the MDN docs.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee