elk/composables/time.ts

42 lines
1.6 KiB
TypeScript
Raw Normal View History

import type { MaybeComputedRef, UseTimeAgoOptions } from '@vueuse/core'
export const useFormattedDateTime = (
2022-11-27 08:48:04 +00:00
value: MaybeComputedRef<string | Date | undefined | null>,
options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' },
) => {
2022-11-29 23:25:29 +00:00
const { locale } = useI18n()
const formatter = $computed(() => Intl.DateTimeFormat(locale.value, options))
return computed(() => {
const v = resolveUnref(value)
return v ? formatter.format(new Date(v)) : ''
})
}
2022-11-26 05:05:44 +00:00
2022-12-02 08:16:06 +00:00
export const useTimeAgoOptions = (short = false): UseTimeAgoOptions<false> => {
2022-12-02 02:18:36 +00:00
const { d, t } = useI18n()
2022-12-02 08:16:06 +00:00
const prefix = short ? 'short_' : ''
2022-12-02 02:18:36 +00:00
return {
2022-12-02 08:16:06 +00:00
showSecond: !short,
updateInterval: short ? 60_000 : 1_000,
2022-12-02 02:18:36 +00:00
messages: {
justNow: t('time_ago_options.just_now'),
// just return the value
past: n => n,
// just return the value
future: n => n,
2022-12-02 08:16:06 +00:00
second: (n, p) => t(`time_ago_options.${prefix}second_${p ? 'past' : 'future'}`, n),
minute: (n, p) => t(`time_ago_options.${prefix}minute_${p ? 'past' : 'future'}`, n),
hour: (n, p) => t(`time_ago_options.${prefix}hour_${p ? 'past' : 'future'}`, n),
day: (n, p) => t(`time_ago_options.${prefix}day_${p ? 'past' : 'future'}`, n),
week: (n, p) => t(`time_ago_options.${prefix}week_${p ? 'past' : 'future'}`, n),
month: (n, p) => t(`time_ago_options.${prefix}month_${p ? 'past' : 'future'}`, n),
year: (n, p) => t(`time_ago_options.${prefix}year_${p ? 'past' : 'future'}`, n),
2022-12-02 02:18:36 +00:00
},
fullDateFormatter(date) {
2022-12-02 08:16:06 +00:00
return d(date, short ? 'short' : 'long')
2022-12-02 02:18:36 +00:00
},
}
2022-11-26 05:05:44 +00:00
}
2022-12-02 08:16:06 +00:00