0

I am making a countdown app using react.

I am managed to create a countdown start from 180 seconds but I want to convert this minute base which starts from 03:00.

Here is the code

const Countdown: React.FC<ICountdownProps> = ({ setVerification, setTimeUp }) => {
  const [seconds, setSeconds] = useState(180);

  useEffect(() => {
    if (seconds > 0) {
      setTimeout(() => setSeconds(seconds - 1), 1000);
    }
  });

  const notVerified = () => {
    setVerification(false);
    setTimeUp(true)
    return ''
  };

  return (
    <div style={{ position: "absolute", right: 0, bottom: 0, padding: "10px" }}>
      {
        seconds !== 0 ? fmtMSS(seconds) : notVerified()
      }
    </div>
  );
};

export default Countdown;

Can I use moment.js to convert a single number of seconds into a string showing minutes and seconds?

Multihunter
  • 5,520
  • 2
  • 25
  • 38
GoonGamja
  • 1,936
  • 5
  • 20
  • 46
  • Whether you should use moment.js is opinion-based, and shouldn't be answered. I have changed the extra question to not be opinion-based. – Multihunter Nov 19 '20 at 04:55

1 Answers1

1

To calculate you can use this:

const seconds = 180;
const countdown = `${seconds/60}:${seconds%60}`
Raeisi
  • 1,647
  • 1
  • 6
  • 18