Last active
March 21, 2022 02:36
-
-
Save yunyuyuan/4175256b35ba7c25ec57a1f86ee0a199 to your computer and use it in GitHub Desktop.
日期差友好显示
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
function parseDate(stamp) { | |
const dayOld = dayjs.utc(stamp); | |
const dayNew = dayjs.utc(); | |
const subDay = dayNew.diff(dayOld, 'day'); | |
const subWeek = dayNew.diff(dayOld, 'week'); | |
const subMonth = dayNew.diff(dayOld, 'month'); | |
const subYear = dayNew.diff(dayOld, 'year'); | |
if (dayOld.isToday()) { | |
return '今天' | |
} | |
if (subWeek < 1) { | |
return `${subDay+1}天前` | |
} | |
if (subMonth < 1) { | |
return `${subWeek}周前` | |
} | |
if (subYear < 1) { | |
return `${subMonth}个月前` | |
} | |
return `${subYear}年前` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment