import React, { useState, useEffect } from "react";
import "./index.css";

export default (params) => {
  const [clock, setClock] = useState({ time: "", date: "" });
  useEffect(() => {
    var week = [
      "星期日",
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六",
    ];
    var timerID = setInterval(updateTime, 1000);
    updateTime();
    function updateTime() {
      var cd = new Date();
      let time =
        zeroPadding(cd.getHours(), 2) +
        ":" +
        zeroPadding(cd.getMinutes(), 2) +
        ":" +
        zeroPadding(cd.getSeconds(), 2);
      let date =
        zeroPadding(cd.getFullYear(), 4) +
        "-" +
        zeroPadding(cd.getMonth() + 1, 2) +
        "-" +
        zeroPadding(cd.getDate(), 2);
      setClock({
        time,
        date,
      });
    }

    function zeroPadding(num, digit) {
      var zero = "";
      for (var i = 0; i < digit; i++) {
        zero += "0";
      }
      return (zero + num).slice(-digit);
    }
  }, []);
  return (
    <>
      <div className="clock">
        <p className="date">{clock.date}</p>
        <p className="time">{clock.time}</p>
      </div>
    </>
  );
};
