mirror of
https://git.ddmt.top/ddmt/ddmt-tool.git
synced 2026-03-23 05:17:06 +00:00
0.0.1-beta.2
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author: ddmt
|
||||
* @Date: 2024-9-29 20:50:12
|
||||
* @LastEditTime: 2024-9-29 20:50:12
|
||||
* @LastEditTime: 2024-9-30 0:13:12
|
||||
* @LastEditors: ddmt
|
||||
* @Description: ddmt-index file
|
||||
* @FilePath: /Tool/number.js
|
||||
@@ -29,3 +29,43 @@ export function nextArray(arr, index) {
|
||||
index--;
|
||||
return arr.slice(index + 1, arr.length).concat(arr.slice(0, index + 1));
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取相对时间(中文)
|
||||
* @param {Date} date
|
||||
*/
|
||||
export function getRelativeTime(date) {
|
||||
const now = new Date();
|
||||
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
|
||||
|
||||
if (diffInSeconds < 60) {
|
||||
return '刚刚';
|
||||
}
|
||||
|
||||
const diffInMinutes = Math.floor(diffInSeconds / 60);
|
||||
if (diffInMinutes < 60) {
|
||||
return `${diffInMinutes}分钟前`;
|
||||
}
|
||||
|
||||
const diffInHours = Math.floor(diffInMinutes / 60);
|
||||
if (diffInHours < 24) {
|
||||
return `${diffInHours}小时前`;
|
||||
}
|
||||
|
||||
const diffInDays = Math.floor(diffInHours / 24);
|
||||
if (diffInDays < 30) {
|
||||
return `${diffInDays}天前`;
|
||||
}
|
||||
|
||||
const diffInMonths = Math.floor(diffInDays / 30);
|
||||
if (diffInMonths < 12) {
|
||||
return `${diffInMonths}个月前`;
|
||||
}
|
||||
|
||||
const diffInYears = Math.floor(diffInMonths / 12);
|
||||
if (diffInYears < 5) {
|
||||
return `${diffInYears}年前`;
|
||||
}
|
||||
|
||||
return '很久以前';
|
||||
}
|
||||
Reference in New Issue
Block a user