woodpecker/web/src/compositions/useDate.ts
renovate[bot] 22414744b0
chore(deps): update dependency eslint to v9 - abandoned (#3594)
Co-authored-by: qwerty287 <qwerty287@posteo.de>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-06-06 15:16:59 +02:00

51 lines
1.3 KiB
TypeScript

import dayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import duration from 'dayjs/plugin/duration';
import relativeTime from 'dayjs/plugin/relativeTime';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
import { useI18n } from 'vue-i18n';
dayjs.extend(timezone);
dayjs.extend(utc);
dayjs.extend(advancedFormat);
dayjs.extend(relativeTime);
dayjs.extend(duration);
export function useDate() {
function toLocaleString(date: Date) {
return dayjs(date).format(useI18n().t('time.template'));
}
function timeAgo(date: Date | string | number) {
return dayjs().to(dayjs(date));
}
function prettyDuration(durationMs: number) {
return dayjs.duration(durationMs).humanize();
}
const addedLocales = ['en'];
async function setDayjsLocale(locale: string) {
if (!addedLocales.includes(locale)) {
const l = (await import(`~/assets/dayjsLocales/${locale}.js`)) as { default: string };
dayjs.locale(l.default);
} else {
dayjs.locale(locale);
}
}
function durationAsNumber(durationMs: number): string {
const dur = dayjs.duration(durationMs);
return dur.format(dur.hours() > 1 ? 'HH:mm:ss' : 'mm:ss');
}
return {
toLocaleString,
timeAgo,
prettyDuration,
setDayjsLocale,
durationAsNumber,
};
}