-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCountdown.js
More file actions
41 lines (33 loc) · 1.24 KB
/
Countdown.js
File metadata and controls
41 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useState, useEffect } from "react";
import "../CSS/Home.css";
const Countdown = ({ eventName, eventDate }) => {
const calculateTimeLeft = () => {
const eventTime = new Date(eventDate).getTime();
const currentTime = Date.now();
return eventTime - currentTime;
};
const [timeLeft, setTimeLeft] = useState(calculateTimeLeft);
useEffect(() =>{
const countdown = setInterval(() => {
setTimeLeft(calculateTimeLeft());
})
return () => clearInterval(countdown);
},[eventDate]);
const formatTime = (msTime) => {
const totalSeconds = Math.floor(msTime / 1000);
const days = Math.floor(totalSeconds / (3600 * 24));
const hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
const minutes = Math.floor((totalSeconds % (3600)) / 60);
const seconds = Math.floor(totalSeconds % 60)
return `${days}d ${hours}h ${minutes}m ${seconds}s`
}
return (
<div className = "mainPage-body">
<h1 className="summaryTitle">Countdown to {eventName}</h1>
<p className = "summary">
{timeLeft > 0 ? `Time left: ${formatTime(timeLeft)}` : "Time's up!"}
</p>
</div>
);
};
export default Countdown;