/**
* 格式化时间戳
* @param {*} timestamp
* @returns
*/
export function formatDate(timestamp, part) {
if (!timestamp) {
return;
}
let date = new Date(parseInt(timestamp));
let year = date.getFullYear(); // 年
let month = date.getMonth() + 1; // 月
let day = date.getDate(); // 日
let hour = date.getHours(); // 时
let minutes = date.getMinutes(); // 分
let seconds = date.getSeconds(); // 秒
let weekArr = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
];
let week = weekArr[date.getDay()];
// 需要给一位数前面加 0
// 9 点 ----> 09:45:03
if (month >= 1 && month <= 9) {
// month += '0'; // a += b ----> a = a + b
month = "0" + month;
}
if (day >= 0 && day <= 9) {
day = "0" + day;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
var str = "";
switch (part) {
case "year": {
str = `${year}-${month}-${day}`;
break;
}
case "time": {
str = `${hour}:${minutes}:${seconds} `;
break;
}
case "year-time": {
str = `${year}-${month}-${day} ${hour}:${minutes}:${seconds}`;
break;
}
case "time-week": {
str = `${hour}:${minutes}:${seconds} ${week}`;
break;
}
default: {
str = `${year}-${month}-${day} ${hour}:${minutes}:${seconds} ${week}`;
}
}
return str;
}
在组件中使用:
```js
import { useEffect } from "react";
import { formatDate } from "../utils/tools"
// 高阶组件是一个函数,接收一个组件作为参数
// 返回一个新的组件
function withLog(Com) {
// 返回的新组件
return function NewCom(props) {
// 抽离的公共逻辑
useEffect(() => {
console.log(`日志:组件${Com.name}已经创建,创建时间${formatDate(Date.now(),"year-time")}`);
return function(){
console.log(`日志:组件${Com.name}已经销毁,销毁时间${formatDate(Date.now(),"year-time")}`);
}
},[]);
// 一般来讲,传入的组件会作为新组件的视图
return <Com {...props}/>;
};
}
export default withLog;