From 4cfe3ebfaaa28159d91c7bbffe9997ab2d1e8b42 Mon Sep 17 00:00:00 2001 From: Kakabay <2kakabayashyrberdyew@gmail.com> Date: Wed, 11 Dec 2024 18:14:43 +0500 Subject: [PATCH] Calculate time util function added --- lib/hooks/useCalculateTimeLeft.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/hooks/useCalculateTimeLeft.ts diff --git a/lib/hooks/useCalculateTimeLeft.ts b/lib/hooks/useCalculateTimeLeft.ts new file mode 100644 index 0000000..16e3a50 --- /dev/null +++ b/lib/hooks/useCalculateTimeLeft.ts @@ -0,0 +1,15 @@ +export const calculateTimeLeft = (targetDate: string) => { + const now = new Date(); + const eventDate = new Date(targetDate); // Parse target date directly + const timeDifference = eventDate.getTime() - now.getTime(); + + if (timeDifference <= 0) { + return { hours: 0, minutes: 0, seconds: 0 }; // Countdown finished + } + + const hours = Math.floor(timeDifference / (1000 * 60 * 60)); + const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000); + + return { hours, minutes, seconds }; +};