From a48211643a2efea1bc7b11bb7f799536b86c54cf Mon Sep 17 00:00:00 2001
From: unknown <051.sys113@gmail.com>
Date: Wed, 6 Mar 2024 23:12:16 +0330
Subject: [PATCH 1/3] fix: fix countdown without using moment
---
src/app/components/CountDown.tsx | 47 ++++++++++++++++++++------------
1 file changed, 30 insertions(+), 17 deletions(-)
diff --git a/src/app/components/CountDown.tsx b/src/app/components/CountDown.tsx
index a954df5..b2cc674 100644
--- a/src/app/components/CountDown.tsx
+++ b/src/app/components/CountDown.tsx
@@ -1,7 +1,6 @@
"use client";
import { conferenceDateTime } from "@/data/timing";
-import moment from "moment";
import Wave from "@/assets/images/home/count-down-wave.svg";
import WaveMobile from "@/assets/images/home/count-down-wave-mobile.svg";
import Image from "next/image";
@@ -24,26 +23,40 @@ export default function CountDown() {
- تا شروع دومین همایش فرانت چپتر + گذشته از آخرین دورهمی حضوری
); -} +} const CountDownTimer = () => { + const calculateDuration = (start: number, end: number) => { + const totalSeconds = Math.floor((end - start) / 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, + hours, + minutes, + seconds, + }; + }; + const [now, setNow] = useState(Date.now()); - const duration = moment.duration( - Math.max(moment(conferenceDateTime).diff(now), 0), - ); + const conferenceDate = conferenceDateTime.getTime(); + const duration = calculateDuration(now, conferenceDate); - /** - * Update the current time every second - */ + // Update the current time every second useEffect(() => { const interval = setInterval(() => { setNow(Date.now()); @@ -54,32 +67,32 @@ const CountDownTimer = () => { const times = [ { label: "ثانیه", - value: duration.seconds(), + value: duration.seconds, }, { label: "دقیقه", - value: duration.minutes(), + value: duration.minutes, }, { label: "ساعت", - value: duration.hours(), + value: duration.hours, }, { label: "روز", - value: Math.trunc(duration.asDays()), + value: duration.days, }, ]; return (- گذشته از آخرین دورهمی حضوری + از همایش سال ۱۴۰۲ گذشته
); -} +} const CountDownTimer = () => { const calculateDuration = (start: number, end: number) => {