From 9a623164e4a6708a49b583c4d722727390493a89 Mon Sep 17 00:00:00 2001 From: Alikia2x Date: Sun, 31 Mar 2024 01:07:38 +0800 Subject: [PATCH] feature: time --- components/index.tsx | 12 ++++++------ components/time.tsx | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 components/time.tsx diff --git a/components/index.tsx b/components/index.tsx index fcc8ba6..b400adc 100644 --- a/components/index.tsx +++ b/components/index.tsx @@ -1,17 +1,17 @@ +'use client'; + import { useRecoilState } from "recoil"; import Background from "./background"; import Search from "./search/search"; import { bgFocusState } from "./state/background"; +import Time from "./time"; export default function Homepage() { const [isFocus, setFocus] = useRecoilState(bgFocusState); return (
- setFocus(false)} - /> +
); -} \ No newline at end of file +} diff --git a/components/time.tsx b/components/time.tsx new file mode 100644 index 0000000..21becc0 --- /dev/null +++ b/components/time.tsx @@ -0,0 +1,41 @@ +'use client'; + +import React, {useState, useEffect} from "react"; + +export default function Time(props: { + showSecond: boolean; +}){ + const [currentTime, setCurrentTime] = useState(new Date()); + + useEffect(() => { + const timer = setInterval(() => { + setCurrentTime(new Date()); + }, 150); + + return () => { + clearInterval(timer); + }; + }, []); + + const formatTime = () => { + const hours = currentTime.getHours().toString().padStart(2, "0"); + const minutes = currentTime.getMinutes().toString().padStart(2, "0"); + const seconds = currentTime.getSeconds().toString().padStart(2, "0"); + + if (props.showSecond) { + return `${hours}:${minutes}:${seconds}`; + } else { + return `${hours}:${minutes}`; + } + }; + + return ( +
+ {formatTime()} +
+ ); +};