From 6e65712de61f7f850761cb0bf759599fa73e0a74 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 19 Jul 2021 10:24:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B6=88=E6=81=AF=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E6=97=B6=E9=97=B4=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/package.json | 1 + shared/utils/__tests__/date-helper.spec.ts | 21 +++++++++++++++++ shared/utils/date-helper.ts | 26 ++++++++++++++++++++++ yarn.lock | 5 +++++ 4 files changed, 53 insertions(+) create mode 100644 shared/utils/__tests__/date-helper.spec.ts create mode 100644 shared/utils/date-helper.ts diff --git a/shared/package.json b/shared/package.json index 723679df..4d04ce7b 100644 --- a/shared/package.json +++ b/shared/package.json @@ -10,6 +10,7 @@ "@reduxjs/toolkit": "^1.6.0", "axios": "^0.21.1", "crc": "^3.8.0", + "dayjs": "^1.10.6", "formik": "^2.2.9", "i18next": "^20.3.2", "i18next-http-backend": "^1.2.6", diff --git a/shared/utils/__tests__/date-helper.spec.ts b/shared/utils/__tests__/date-helper.spec.ts new file mode 100644 index 00000000..5e4085d5 --- /dev/null +++ b/shared/utils/__tests__/date-helper.spec.ts @@ -0,0 +1,21 @@ +import { isToday, getMessageTimeDiff } from '../date-helper'; + +describe('isToday', () => { + test.each([ + [new Date(), true], + [new Date(new Date().setDate(new Date().getDate() - 1)), false], + ])('%s => %s', (input, should) => { + expect(isToday(input)).toBe(should); + }); +}); + +describe('getMessageTimeDiff', () => { + test.each([ + [new Date(), '几秒前'], + [new Date(new Date().setMinutes(new Date().getMinutes() - 1)), '1 分钟前'], + [new Date(new Date().setHours(new Date().getHours() - 1)), '1 小时前'], + [new Date('2020-01-01T00:00:00Z'), '2020-01-01 08:00:00'], + ])('%s => %s', (input, should) => { + expect(getMessageTimeDiff(input)).toBe(should); + }); +}); diff --git a/shared/utils/date-helper.ts b/shared/utils/date-helper.ts new file mode 100644 index 00000000..66a45992 --- /dev/null +++ b/shared/utils/date-helper.ts @@ -0,0 +1,26 @@ +import dayjs from 'dayjs'; +import relativeTime from 'dayjs/plugin/relativeTime'; // 导入插件 +import 'dayjs/locale/zh-cn'; // 导入本地化语言 + +dayjs.extend(relativeTime); +dayjs.locale('zh-cn'); + +/** + * 是否为当天 + */ +export function isToday(date: Date): boolean { + return dayjs(date).isSame(dayjs(), 'd'); +} + +/** + * 获取消息显示时间 + */ +export function getMessageTimeDiff(input: Date): string { + const date = dayjs(input); + + if (isToday(input)) { + return date.fromNow(); + } else { + return date.format('YYYY-MM-DD HH:mm:ss'); + } +} diff --git a/yarn.lock b/yarn.lock index c7e37bfe..f9a64983 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2941,6 +2941,11 @@ date-fns@^2.15.0: resolved "https://registry.npmjs.org/date-fns/-/date-fns-2.22.1.tgz#1e5af959831ebb1d82992bf67b765052d8f0efc4" integrity sha512-yUFPQjrxEmIsMqlHhAhmxkuH769baF21Kk+nZwZGyrMoyLA+LugaQtC0+Tqf9CBUUULWwUJt6Q5ySI3LJDDCGg== +dayjs@^1.10.6: + version "1.10.6" + resolved "https://registry.npmjs.org/dayjs/-/dayjs-1.10.6.tgz#288b2aa82f2d8418a6c9d4df5898c0737ad02a63" + integrity sha512-AztC/IOW4L1Q41A86phW5Thhcrco3xuAA+YX/BLpLWWjRcTj5TOt/QImBLmCKlrF7u7k47arTnOyL6GnbG8Hvw== + debug@2.6.9, debug@^2.2.0, debug@^2.3.3: version "2.6.9" resolved "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"