import { useState } from "react";
function Test() {
const [date, setDate] = useState('');
const getNewDate = () => {
const time = new Date();
const year = time.getFullYear(); //年
const month = time.getMonth() + 1; //月
const day = time.getDate(); //日
const hour = time.getHours(); //时
const minutes = time.getMinutes();//分
const s = time.getSeconds(); //秒
const seconds = s <= 9 ? "0" + s : s;
const t = year + '年' + month + '月' + day + "日" + " " + hour + ":" + minutes + ":"
+ seconds;
setDate(t);
}
setInterval(getNewDate, 1000);
return <div>{date}</div>
}
export default Test;
react 实现实时展示本地时间
最新推荐文章于 2023-10-11 09:54:45 发布