toshiki-notebook/docs/.vitepress/theme/utils/date.ts

27 lines
759 B
TypeScript

// data format transofmation utility for both feed and pageinfo generation
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc.js'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/en'
dayjs.extend(utc)
dayjs.locale('en')
dayjs.extend(relativeTime)
export function getDate(date: string | Date | undefined): string | null {
if (date) {
const time = dayjs(date instanceof Date ? date : date.trim())
if (time.isValid()) {
const currentTime = dayjs(date).utc().local().format('YYYY-MM-DD')
return currentTime
}
}
return null
}
export function getFromNow(date: string | Date): string | null {
if (date) return dayjs(date).utc().local().fromNow()
return null
}