import { useEffect, useState } from 'react'
import dayjs from 'dayjs'
import { px2vh } from '@/utils/styleUtils'
export function ElectronicClock() {
const [currentDateTime, setCurrentDateTime] = useState(new Date())
useEffect(() => {
const intervalId = setInterval(() => {
setCurrentDateTime(new Date())
}, 1000)
return () => {
clearInterval(intervalId)
}
}, [])
return (
<span className="electronic-clock" style={{ fontSize: px2vh(16) }}>
{dayjs(currentDateTime.toLocaleString()).format('YYYY-MM-DD HH:mm:ss')}
</span>
)
}
react 做一个实时的日期时间计时器
于 2023-12-08 14:44:18 首次发布