Solution

To get current date in React, you can use the new Date() object it will return current date & time based on system timezone.

To format a date you can read my below article, here I have explained multiple ways to format a date.

https://reactjssnippet.com/posts/how-to-display-date-in-react-js/

Snippet

In this snippet, we will see short example to genrate current date in react js.

const date = new Date();

console.log("Date", date.toLocaleDateString()) // 12/27/2022

Example

In this example, we will write a react js code to get current date and show in the React UI.

Let’s list down what we are going to do below example.

  • Create currentDate state
  • set current date in state when page load ( useEffect )
  • format date using toLocaleDateString() and show in the UI.
import React, { useState, useEffect } from "react";
import moment from "moment";

export default function App() {
  const [currentDate, setCurrentDate] = useState("");

  useEffect(() => {
    setCurrentDate(new Date());
  }, []);

  return (
    <div className="App">
      <h1>Current Date</h1>
      <p>{currentDate ? currentDate.toLocaleDateString() : ""}</p>
    </div>
  );
}

Output

current, date

codesandbox