mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-30 18:00:56 +01:00
29 lines
826 B
TypeScript
29 lines
826 B
TypeScript
import dayjs from 'dayjs';
|
|
import TimeAgo, { LocaleData, FormatStyleName } from 'javascript-time-ago';
|
|
import { getI18n } from '@/i18n';
|
|
import en from 'javascript-time-ago/locale/en';
|
|
import zh from 'javascript-time-ago/locale/zh';
|
|
|
|
TimeAgo.addLocale(en as LocaleData);
|
|
TimeAgo.addLocale(zh as LocaleData);
|
|
|
|
export const getTimeUnitParts = (timeUnit: string) => {
|
|
const groups = timeUnit.match(/(\d+)([a-z])/);
|
|
if (!groups) return {};
|
|
const num = parseInt(groups[1]);
|
|
const unit = groups[2];
|
|
return { num, unit };
|
|
};
|
|
|
|
export const formatTimeAgo = (
|
|
value: string | Date,
|
|
formatStyle?: string | FormatStyleName
|
|
) => {
|
|
const time = dayjs(value);
|
|
const timeAgo = new TimeAgo(
|
|
getI18n().global.locale.value === 'zh' ? 'zh' : 'en'
|
|
);
|
|
// @ts-ignore
|
|
return timeAgo.format(time.toDate(), formatStyle);
|
|
};
|