53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import dayjs from 'dayjs';
|
|
|
|
export function formatTime(time: number | string, template = 'YYYY-MM-DD', toNum = true): string {
|
|
if (!time) {
|
|
return '';
|
|
}
|
|
if (toNum) {
|
|
time = Number(time);
|
|
}
|
|
return dayjs(time).format(template);
|
|
}
|
|
|
|
export function formatDate(time: number | string, template = 'YYYY-MM-DD'): string {
|
|
const now = dayjs();
|
|
const oneHour = now.subtract(1, 'hour').valueOf();
|
|
time = Number(time);
|
|
if (time > oneHour) {
|
|
return '刚刚';
|
|
}
|
|
const todayStart = now.startOf('date').valueOf();
|
|
if (time > todayStart) {
|
|
return '今天';
|
|
}
|
|
const yesterdayStart = now.subtract(1, 'days').startOf('date').valueOf();
|
|
if (time > yesterdayStart) {
|
|
return '昨天';
|
|
}
|
|
return dayjs(time).format(template);
|
|
}
|
|
|
|
export function activeDate(time: number | string): string {
|
|
const now = dayjs();
|
|
time = Number(time);
|
|
// const oneHour = now.subtract(1, 'hour').valueOf();
|
|
// if (time > oneHour) {
|
|
// return '刚刚活跃';
|
|
// }
|
|
// const todayStart = now.startOf('date').valueOf();
|
|
// if (time > todayStart) {
|
|
// return '今天活跃';
|
|
// }
|
|
const yesterdayStart = now.subtract(3, 'days').startOf('date').valueOf();
|
|
if (time > yesterdayStart) {
|
|
return '3日内活跃';
|
|
}
|
|
|
|
const weakStart = now.subtract(1, 'weeks').startOf('date').valueOf();
|
|
if (time > weakStart) {
|
|
return '7日内活跃';
|
|
}
|
|
return '';
|
|
}
|