-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { addDays, addYears, subDays, subYears } from 'date-fns/esm' | ||
import { formatDistancePlus } from './formatDistancePlus' | ||
|
||
describe('formatDistancePlus', () => { | ||
test('前', () => { | ||
expect(formatDistancePlus(subDays(new Date(), 1))).toBe('1天前') | ||
expect(formatDistancePlus(subDays(new Date(), 1.5))).toBe('1天前') | ||
expect(formatDistancePlus(subYears(new Date(), 10))).toBe('10年前') | ||
|
||
expect( | ||
formatDistancePlus(subDays(new Date(), 1), addDays(new Date(), 2)), | ||
).toBe('3天前') | ||
expect( | ||
formatDistancePlus(subDays(new Date(), 1.5), addDays(new Date(), 2)), | ||
).toBe('3天前') | ||
expect( | ||
formatDistancePlus(subYears(new Date(), 10), addYears(new Date(), 3)), | ||
).toBe('13年前') | ||
}) | ||
|
||
test('后', () => { | ||
expect(formatDistancePlus(addDays(new Date(), 1))).toBe('1天后') | ||
expect(formatDistancePlus(addDays(new Date(), 1.5))).toBe('1天后') | ||
expect(formatDistancePlus(addYears(new Date(), 10))).toBe('10年后') | ||
|
||
expect( | ||
formatDistancePlus(addDays(new Date(), 1), subDays(new Date(), 1)), | ||
).toBe('2天后') | ||
expect( | ||
formatDistancePlus(addDays(new Date(), 1.5), subDays(new Date(), 1)), | ||
).toBe('2天后') | ||
expect( | ||
formatDistancePlus(addYears(new Date(), 10), subYears(new Date(), 10)), | ||
).toBe('20年后') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { formatDistanceStrict, isBefore } from 'date-fns/esm' | ||
import { zhCN } from 'date-fns/esm/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}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters