Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 12x 12x 12x | import { formatDistanceStrict, isBefore } from 'date-fns'
import { zhCN } from 'date-fns/locale'
/**
* 将时间转化为 `xxx前/后` 表示。
*
* @param date 时间
* @param baseDate 基准时间(默认当前时间)
*/
export function formatDistancePlus(
date: Date,
baseDate: Date = new Date(),
): string {
const suffix = isBefore(date, baseDate) ? '前' : '后'
const distance = formatDistanceStrict(date, baseDate, {
locale: zhCN,
}).replace(/\s+/g, '')
return `${distance}${suffix}`
}
|