Calculate time util function added

This commit is contained in:
Kakabay 2024-12-11 18:14:43 +05:00
parent f28671f99e
commit 4cfe3ebfaa
1 changed files with 15 additions and 0 deletions

View File

@ -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 };
};