2022-11-26 09:34:24 +01:00
|
|
|
import type { MaybeComputedRef, UseTimeAgoOptions } from '@vueuse/core'
|
2022-11-25 09:57:34 +01:00
|
|
|
|
|
|
|
export const useFormattedDateTime = (
|
2022-11-27 09:48:04 +01:00
|
|
|
value: MaybeComputedRef<string | Date | undefined | null>,
|
2022-11-25 09:57:34 +01:00
|
|
|
options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' },
|
|
|
|
) => {
|
2022-11-30 00:25:29 +01:00
|
|
|
const { locale } = useI18n()
|
2022-12-01 14:14:58 +01:00
|
|
|
const formatter = $computed(() => Intl.DateTimeFormat(locale.value, options))
|
2022-11-26 04:36:18 +01:00
|
|
|
return computed(() => {
|
|
|
|
const v = resolveUnref(value)
|
|
|
|
return v ? formatter.format(new Date(v)) : ''
|
|
|
|
})
|
2022-11-25 09:57:34 +01:00
|
|
|
}
|
2022-11-26 06:05:44 +01:00
|
|
|
|
2022-12-02 03:18:36 +01:00
|
|
|
export const useTimeAgoOptions = (): UseTimeAgoOptions<false> => {
|
|
|
|
const { d, t } = useI18n()
|
|
|
|
|
|
|
|
return {
|
|
|
|
showSecond: true,
|
|
|
|
updateInterval: 1_000,
|
|
|
|
messages: {
|
|
|
|
justNow: t('time_ago_options.just_now'),
|
|
|
|
// just return the value
|
|
|
|
past: n => n,
|
|
|
|
// just return the value
|
|
|
|
future: n => n,
|
|
|
|
second: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_second`, n),
|
|
|
|
minute: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_minute`, n),
|
|
|
|
hour: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_hour`, n),
|
|
|
|
day: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_day`, n),
|
|
|
|
week: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_week`, n),
|
|
|
|
month: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_month`, n),
|
|
|
|
year: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_year`, n),
|
|
|
|
},
|
|
|
|
fullDateFormatter(date) {
|
|
|
|
return d(date, 'long')
|
|
|
|
},
|
|
|
|
}
|
2022-11-26 06:05:44 +01:00
|
|
|
}
|