Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138249

format difference in moment

$
0
0

I have some code. Now the result of {time} is 1000 in milliseconds. I need to convert those millisecond to format like 00:00:01. I know that i should use moment().format('hh:mm:ss'), but the problem is that i dont know where i can use it to make it work correctly. Ty for your help!

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import moment from "moment";

import "./styles.css";

const App = () => {
  const [time, setTime] = useState('');

  useEffect(() => {
    reqEmulation();
  });

  const reqEmulation = async () => {
    const timeStart = moment();
    const promise = new Promise(res => {
      setTimeout(() => {
        const timeEnd = moment();
        res(timeEnd.diff(timeStart));
      }, 1000);
    });
    let result = await promise;
    setTime(result);
  };

  return (
    <div className="App">
      <p>{time}</p>
    </div>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

I expect the output 00:00:01, but the actual output is 1000 milliseconds.


Viewing all articles
Browse latest Browse all 138249

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>