Commit 99d3ed39 authored by 崔佳豪's avatar 崔佳豪

feat: useTime

parent bc78773b
Pipeline #48272 skipped with stages
import { useEffect, useState } from 'react';
/**
* 自定义hook
* 登录页时间
* @returns {object} 返回时间信息,包含time、dayofweek、date
*/
const useTime = () => {
const [currentDate, setCurrentDate] = useState({});
useEffect(() => {
let loginTimeInterval = null;
if (loginTimeInterval) clearInterval(loginTimeInterval), (loginTimeInterval = null);
loginTimeInterval = setInterval(() => {
const date = new Date();
const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
const time = `${date.getHours() < 10 ? `0${date.getHours()}` : date.getHours()}:${
date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes()
}:${date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds()}`;
setCurrentDate({
time,
dayofweek: weekday[date.getDay()],
date: date.pattern('yyyy/MM/dd'),
});
}, 1000);
return () => {
loginTimeInterval && clearInterval(loginTimeInterval);
};
}, []);
return currentDate;
};
export default useTime;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment