Solution

To convert epoch time to date in React, you can use the new Date() method which will return epoch time as a date object.

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 take epoch time and use new Date() to convert in date.

// Get epoch time
const epochTime = 1609459200000;  // This is Fri Jan 01 2021 in epoch time

// convert epoch time to date
const date = new Date(epochTime);

console.log(date);

Example

In this example, we will take a sample epoch time constant and convert it into a date and show it in the UI.

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

  • Create a state with sample epoch time data.
  • Use the new Date() constructor to convert epoch time into a date.
  • Show in the react UI in human-readable format.

For more formatting options learn - https://reactjssnippet.com/posts/how-to-display-date-in-react-js/

import React, { useState } from "react";

export default function App() {
  const [epochTime, setEpochTime] = useState(1609459200000);

  return (
    <div className="App">
      <h1>{`Epoch Time to Date`}</h1>
      <p>
        {epochTime}
        {` => `}
        {new Date(epochTime).toDateString()}
      </p>
    </div>
  );
}

Output

epoch, date

codesandbox