get last year from the current date in javascript

You can use the built-in Date object in JavaScript to get the current date, subtract a year from it, and then extract the year value. This can be done using the following code:

index.tsx
const today = new Date(); // get current date
const lastYear = today.getFullYear() - 1; // subtract one year and get the year value
console.log(lastYear); // outputs the year value of last year
194 chars
4 lines

Note that getFullYear() method returns the year value of a Date object, as a four-digit number.

related categories

gistlibby LogSnag