Skip to content

Commit e8f1c25

Browse files
committed
fix: 修改时间显示
1 parent 401ef84 commit e8f1c25

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/components/LoginLog/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { findAccountLogs } from "@/api/caravan/Login";
22
import { LoginFrequencyChart } from "@/components/Echarts";
3+
import { getDateTimeFormat } from "@/utils/core";
34
import { Table } from "antd";
45
import dayjs from "dayjs";
56
import { useEffect, useState } from "react";
@@ -44,7 +45,7 @@ const LoginLog = () => {
4445
title: "登录时间",
4546
dataIndex: "created",
4647
render: item => {
47-
return <span>{dayjs(item).format("YYYY-MM-DD HH:mm")}</span>;
48+
return <span>{getDateTimeFormat(new Date(item).valueOf())}</span>;
4849
}
4950
}
5051
];

src/utils/core.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,42 @@ export const getMenuStructure = (menus: any[], parentUid: number = 0) => {
250250
});
251251
return newList.sort((a, b) => a.sort - b.sort);
252252
};
253+
254+
/* *
255+
* 把传入的时间戳与当前时间比较,计算几分钟前、几小时前、几天前,以及几分钟后、几小时后、几天前后
256+
* unixtime 需要计算的时间戳,保留到秒
257+
* */
258+
export const getDateTimeFormat = unixtime => {
259+
const calcTime = typeof unixtime === "string" ? new Date(unixtime).valueOf() : unixtime;
260+
const currTime = new Date().valueOf();
261+
const time = currTime - calcTime;
262+
263+
const value = Math.abs(time / 1000);
264+
// 少于一分钟
265+
if (value < 60) return "刚刚";
266+
267+
// 秒转分钟
268+
const minuies = value / 60;
269+
if (minuies < 60) {
270+
return Math.floor(minuies) + "分钟前";
271+
}
272+
273+
// 秒转小时
274+
const hours = value / 3600;
275+
if (hours < 24) {
276+
return Math.floor(hours) + "小时前";
277+
}
278+
//秒转天数
279+
const days = value / 3600 / 24;
280+
if (days < 30) {
281+
return Math.floor(days) + "天前";
282+
}
283+
//秒转月
284+
const months = value / 3600 / 24 / 30;
285+
if (months < 12) {
286+
return Math.floor(months) + "月前";
287+
}
288+
//秒转年
289+
const years = value / 3600 / 24 / 30 / 12;
290+
return Math.floor(years) + "年前";
291+
};

0 commit comments

Comments
 (0)