1
0
mirror of https://github.com/elk-zone/elk synced 2024-12-01 00:08:11 +09:00
elk/composables/time.ts
2022-11-26 13:07:01 +08:00

45 lines
1.1 KiB
TypeScript

import type { MaybeRef, UseTimeAgoOptions } from '@vueuse/core'
export const useFormattedDateTime = (
value: MaybeRef<string | Date | undefined>,
options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' },
) => {
const formatter = Intl.DateTimeFormat(undefined, options)
return computed(() => {
const v = resolveUnref(value)
return v ? formatter.format(new Date(v)) : ''
})
}
export const timeAgoOptions: UseTimeAgoOptions<false> = {
showSecond: true,
messages: {
justNow: 'just now',
past: n => n,
future: n => n.match(/\d/) ? `in ${n}` : n,
month: (n, past) => n === 1
? past
? 'last month'
: 'next month'
: `${n}m`,
year: (n, past) => n === 1
? past
? 'last year'
: 'next year'
: `${n}y`,
day: (n, past) => n === 1
? past
? 'yesterday'
: 'tomorrow'
: `${n}d`,
week: (n, past) => n === 1
? past
? 'last week'
: 'next week'
: `${n} week${n > 1 ? 's' : ''}`,
hour: n => `${n}h`,
minute: n => `${n}min`,
second: n => `${n}s`,
},
}