Created
August 27, 2023 08:34
-
-
Save gioiliop7/701ede1aa6d7f35ae6240748ed4614f1 to your computer and use it in GitHub Desktop.
Create digital-clock in React typescript using tailwind.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
const DigitalClock: React.FC = () => { | |
const [time, setTime] = useState(new Date()); | |
useEffect(() => { | |
const interval = setInterval(() => { | |
setTime(new Date()); | |
}, 1000); | |
return () => { | |
clearInterval(interval); | |
}; | |
}, []); | |
if (typeof window !== "undefined") { | |
const formattedTime = time.toLocaleTimeString("en-US", { | |
hour: "2-digit", | |
minute: "2-digit", | |
second: "2-digit", | |
hour12: false, // Display in 24-hour format | |
}); | |
return ( | |
<div className="text-4xl font-semibold" id="digital-clock"> | |
{formattedTime} | |
</div> | |
); | |
} else { | |
return null; | |
} | |
}; | |
export default DigitalClock; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment