feat: first commit
This commit is contained in:
50
src/utils/time.ts
Normal file
50
src/utils/time.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export function formatTime(time: number | string, template = 'YYYY-MM-DD'): string {
|
||||
if (!time) {
|
||||
return '';
|
||||
}
|
||||
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 '';
|
||||
}
|
||||
Reference in New Issue
Block a user