mirror of
https://github.com/LiuYuYang01/ThriveX-Blog.git
synced 2026-06-08 08:12:30 +08:00
功能:调整闪念页面布局
This commit is contained in:
42
src/utils/dayFormat.ts
Normal file
42
src/utils/dayFormat.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
const dayjs = require('dayjs');
|
||||
const relativeTime = require('dayjs/plugin/relativeTime');
|
||||
const localeData = require('dayjs/plugin/localeData');
|
||||
const updateLocale = require('dayjs/plugin/updateLocale');
|
||||
|
||||
dayjs.extend(relativeTime);
|
||||
dayjs.extend(localeData);
|
||||
dayjs.extend(updateLocale);
|
||||
|
||||
// 更新 locale 配置以自定义显示
|
||||
dayjs.updateLocale('en', {
|
||||
relativeTime: {
|
||||
future: 'in %s',
|
||||
past: '%s前',
|
||||
s: '几秒',
|
||||
m: '1 分钟',
|
||||
mm: '%d 分钟',
|
||||
h: '1 小时',
|
||||
hh: '%d 小时',
|
||||
d: '1 天',
|
||||
dd: '%d 天',
|
||||
M: '1 个月',
|
||||
MM: '%d 个月',
|
||||
y: '1 年',
|
||||
yy: '%d 年'
|
||||
}
|
||||
});
|
||||
|
||||
export default function dayFormat(timestamp: number | string) {
|
||||
const now = dayjs();
|
||||
const target = dayjs(+timestamp);
|
||||
|
||||
if (now.isSame(target, 'day')) {
|
||||
return '今天';
|
||||
} else if (now.subtract(1, 'day').isSame(target, 'day')) {
|
||||
return '昨天';
|
||||
} else if (now.subtract(2, 'day').isSame(target, 'day')) {
|
||||
return '前天';
|
||||
} else {
|
||||
return target.fromNow();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
import dayFormat from './dayFormat'
|
||||
|
||||
// 生成随机数
|
||||
export function getRandom(min: number, max: number): number {
|
||||
return Math.floor(Math.random() * (max - min + 1) + min);
|
||||
}
|
||||
}
|
||||
|
||||
export { dayFormat }
|
||||
Reference in New Issue
Block a user