import React, { useEffect, useRef, useState } from 'react';
export default function Timer({ style = {} }: { style?: any }) {
  const [dataNow, setDataNow] = useState('');
  let timer: any = useRef();
  useEffect(() => {
    timer.current = setInterval(() => {
      setDataNow(getDate());
    }, 1000);
    return () => clearInterval(timer.current);
  }, []);
  const getDate = () => {
    const date = new Date();
    var time = new Date();
    var year = time.getFullYear(); //获取年份
    var month = time.getMonth() + 1; //获取月份
    var day = time.getDate(); //获取日期
    var hour = time.getHours().toString().padStart(2, '0'); //获取时
    var minite = time.getMinutes().toString().padStart(2, '0'); //获取分
    var second = time.getSeconds().toString().padStart(2, '0'); //获取秒
    const weekArray = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    const week = weekArray[date.getDay()];
    return `${month}月${day}日 ${week}  ${hour}:${minite}`;
  };
  return <div style={style}>{dataNow}</div>;
}
