Solution

To get the current month in react js, use the new Date().getFullYear() it will return the current year.

getFullYear()

The getFullYear() method returns the year of the specified date according to local time.

Snippet

In this snippet, we will create a date object and get a current year from it.

const date = new Date();

let year = date.getFullYear(); // 2022 ( Sample Output )

Example

In this example, we will show the current year in the console and page in react js.

Let’s start coding…

import React, { useEffect } from "react";
export default function App() {
  useEffect(() => {
    console.log("Current Year", new Date().getFullYear());
  }, []);

  return (
    <div className="App">
      <h1>{`Current Year`}</h1>
      <p>{new Date().getFullYear()}</p>
    </div>
  );
}

Output

Current, Year

codesandbox