import { useState, useEffect } from 'react'; import './BSTImer.scss'; import { ExtractDateFormate } from '../../../../Services/Others'; const useCurrentTime = () => { const [currentTime, setCurrentTime] = useState(new Date()); useEffect(() => { const timer = setInterval(() => setCurrentTime(new Date()), 1000); return () => clearInterval(timer); }, []); return currentTime; }; const LiveTimer = () => { // Initialize state for current time const [currentTime, setCurrentTime] = useState(new Date()); // useEffect to update the current time every second useEffect(() => { const timerID = setInterval(() => tick(), 1000); // Cleanup function to clear interval when the component unmounts return () => { clearInterval(timerID); }; }, []); // Empty dependency array means this effect runs once when the component mounts // Function to update the current time const tick = () => { setCurrentTime(new Date()); }; // Format time to 12-hour format with AM/PM const hours = String(currentTime.getHours() % 12 || 12).padStart(2, '0'); const minutes = String(currentTime.getMinutes()).padStart(2, '0'); const seconds = String(currentTime.getSeconds()).padStart(2, '0'); const ampm = currentTime.getHours() >= 12 ? 'PM' : 'AM'; return (
{hours}:{minutes}:{seconds} {ampm}