Skip to content

Instantly share code, notes, and snippets.

@gioiliop7
Created August 27, 2023 08:34
Show Gist options
  • Save gioiliop7/701ede1aa6d7f35ae6240748ed4614f1 to your computer and use it in GitHub Desktop.
Save gioiliop7/701ede1aa6d7f35ae6240748ed4614f1 to your computer and use it in GitHub Desktop.
Create digital-clock in React typescript using tailwind.css
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