To use the getYear
function from date-fns in Javascript, first you need to install the date-fns package via npm, using the following command:
index.tsx21 chars2 lines
Then, you can import the getYear
function from date-fns, and use it with a Date object to get the year component, like this:
index.tsx139 chars7 lines
In the example above, we import the getYear
function using destructuring, and then create a new Date
object with a specific date. Finally, we call the getYear
function with the myDate
object as an argument, and log the result to the console.
Alternatively, if you're using ES6 modules, you can import the getYear
function like this:
index.tsx36 chars2 lines
And use it in a similar way.
gistlibby LogSnag