From 1ab5215a4294c2bd80443a14088fcd79cbd97d16 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Sat, 5 Oct 2024 16:19:16 -0700 Subject: add countdown timer to date in next milestone projection --- src/components/Countdown.tsx | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/components/Countdown.tsx (limited to 'src/components/Countdown.tsx') diff --git a/src/components/Countdown.tsx b/src/components/Countdown.tsx new file mode 100644 index 0000000..69e4bd0 --- /dev/null +++ b/src/components/Countdown.tsx @@ -0,0 +1,70 @@ +import React, { useEffect, useState } from 'react'; + +interface CountdownProps { + targetDate: string; +} + +const Countdown: React.FC = ({ targetDate }) => { + const calculateTimeLeft = () => { + const difference = new Date(targetDate).getTime() - new Date().getTime(); + let timeLeft = { + days: '0', + hours: '0', + minutes: '0', + seconds: '0', + }; + + if (difference > 0) { + timeLeft = { + days: Math.floor(difference / (1000 * 60 * 60 * 24)).toString(), + hours: Math.floor((difference / (1000 * 60 * 60)) % 24).toString(), + minutes: Math.floor((difference / 1000 / 60) % 60).toString(), + seconds: Math.floor((difference / 1000) % 60).toString(), + }; + } + + return timeLeft; + }; + + const [timeLeft, setTimeLeft] = useState({ + days: '--', + hours: '--', + minutes: '--', + seconds: '--', + }); + + useEffect(() => { + setTimeLeft(calculateTimeLeft()); + + const timer = setInterval(() => { + setTimeLeft(calculateTimeLeft()); + }, 1000); + + return () => clearInterval(timer); + }, [targetDate]); + + return ( +
+
+
+
{timeLeft.days}
+
Days
+
+
+
{timeLeft.hours}
+
Hours
+
+
+
{timeLeft.minutes}
+
Minutes
+
+
+
{timeLeft.seconds}
+
Seconds
+
+
+
+ ); +}; + +export default Countdown; \ No newline at end of file -- cgit v1.2.3