From 2c1d8b20580220c404f37a9a40864283682abd6d Mon Sep 17 00:00:00 2001 From: bianxuerui <407955981@qq.com> Date: Mon, 5 Aug 2024 14:53:21 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9EcomputeAge=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/datetime/computeAge.js | 28 +++++++++++++++++++++++++ lib/datetime/computeAge.test.js | 36 +++++++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 lib/datetime/computeAge.js create mode 100644 lib/datetime/computeAge.test.js diff --git a/lib/datetime/computeAge.js b/lib/datetime/computeAge.js new file mode 100644 index 0000000..784cc1b --- /dev/null +++ b/lib/datetime/computeAge.js @@ -0,0 +1,28 @@ +import dayjs from 'dayjs'; + +/** + * 计算年龄 + * + * @author 卞雪瑞 + * @category 时间操作 + * @alias yd_compute_age + * @param {String} birthday - 生日日期字符串 + * @summary 根据给的生日计算出用户的年龄 + * @returns {Number} - 返回计算出的年龄,如果未提供生日则返回0 + */ +const computeAge = (birthday) => { + if (birthday) { + const _birthday = dayjs(birthday); + + if (!_birthday.isValid()) { + return 0; + } + + const cur = dayjs(); + const age = cur.get('year') - _birthday.get('year') - (cur.get('month') < _birthday.get('month') || (cur.get('month') == _birthday.get('month') && cur.get('date') < _birthday.get('date')) ? 1 : 0); + return age; + } + return 0; +}; + +export default computeAge; diff --git a/lib/datetime/computeAge.test.js b/lib/datetime/computeAge.test.js new file mode 100644 index 0000000..124ea90 --- /dev/null +++ b/lib/datetime/computeAge.test.js @@ -0,0 +1,36 @@ +import { describe, test, expect } from 'vitest'; +import dayjs from 'dayjs'; +import dy_compute_age from './computeAge'; // 确保路径正确 + +describe('dy_compute_age', () => { + test('正确计算年龄', () => { + const birthday = '1990-01-01'; // 例如,出生日期为 1990 年 1 月 1 日 + const expectedAge = dayjs().year() - 1990; // 计算预期的年龄 + const age = dy_compute_age(birthday); + expect(age).toBe(expectedAge); + }); + + test('计算同一天出生的年龄应为 0', () => { + const today = dayjs().format('YYYY-MM-DD'); + const age = dy_compute_age(today); + expect(age).toBe(0); + }); + + test('边界情况:出生日期在今年的某一天', () => { + const birthday = dayjs().subtract(5, 'month').format('YYYY-MM-DD'); // 例如,5 个月前 + const expectedAge = dayjs().year() - dayjs(birthday).year(); + const age = dy_compute_age(birthday); + expect(age).toBe(expectedAge); + }); + + test('无效日期应返回 0', () => { + const invalidDate = 'not-a-date'; + const age = dy_compute_age(invalidDate); + expect(age).toBe(0); + }); + + test('未提供出生日期应返回 0', () => { + const age = dy_compute_age(); + expect(age).toBe(0); + }); +}); diff --git a/package.json b/package.json index c9b4d32..c6ada4b 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "inspect": "eslint --inspect-config" }, "dependencies": { - "date-fns": "^3.6.0" + "date-fns": "^3.6.0", + "dayjs": "^1.11.12" }, "devDependencies": { "@eslint/config-inspector": "^0.5.2", From 0492e9ccdfea11830816325da4840a0950cd815d Mon Sep 17 00:00:00 2001 From: bianxuerui <407955981@qq.com> Date: Mon, 5 Aug 2024 14:53:48 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9EdateRange=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/datetime/dateRange.js | 30 ++++++++++++++++++++++ lib/datetime/dateRange.test.js | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 lib/datetime/dateRange.js create mode 100644 lib/datetime/dateRange.test.js diff --git a/lib/datetime/dateRange.js b/lib/datetime/dateRange.js new file mode 100644 index 0000000..21208bc --- /dev/null +++ b/lib/datetime/dateRange.js @@ -0,0 +1,30 @@ +import dayjs from 'dayjs'; + +/** + * 返回一个时间范围 + * @author 卞雪瑞 + * @category 时间操作 + * @alias yd_date_range + * @param {number} year - 需要增加的年数,默认为0 + * @param {number} month - 需要增加的月数,默认为0 + * @param {number} day - 需要增加的天数,默认为0 + * @param {number} hour - 需要增加的小时数,默认为0 + * @param {number} minute - 需要增加的分钟数,默认为0 + * @param {number} second - 需要增加的秒数,默认为0 + * @summary 生成一个时间范围数组,该数组包含当前时间和目标时间 + * 如果目标时间在当前时间之后,则返回 [当前时间, 目标时间] 的数组 + * 如果目标时间在当前时间之前或相同,则返回 [目标时间, 当前时间] 的数组 + * @returns {Array} 返回一个包含两个dayjs对象的数组,分别代表开始时间和结束时间 + */ + +const dateRange = (year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0) => { + const now = dayjs(); + const targetDate = now.add(year, 'year').add(month, 'month').add(day, 'day').add(hour, 'hour').add(minute, 'minute').add(second, 'second'); + + if (now.isBefore(targetDate)) { + return [now, targetDate]; + } + return [targetDate, now]; +}; + +export default dateRange; diff --git a/lib/datetime/dateRange.test.js b/lib/datetime/dateRange.test.js new file mode 100644 index 0000000..87a43af --- /dev/null +++ b/lib/datetime/dateRange.test.js @@ -0,0 +1,46 @@ +import { describe, test, expect } from 'vitest'; +import dayjs from 'dayjs'; +import yd_date_range from './dateRange'; + +describe('yd_date_range', () => { + test('默认情况下返回当前时间范围', () => { + const [start, end] = yd_date_range(); + expect(start.isSame(dayjs(), 'second')).toBe(true); + expect(end.isSame(dayjs(), 'second')).toBe(true); + }); + + test('增加一年', () => { + const [start, end] = yd_date_range(1); + expect(end.year()).toBe(start.year() + 1); + }); + + test('增加一个月', () => { + const [start, end] = yd_date_range(0, 1); + expect(end.month()).toBe((start.month() + 1) % 12); + }); + + test('增加一天', () => { + const [start, end] = yd_date_range(0, 0, 1); + expect(end.date()).toBe(start.date() + 1); + }); + + test('增加一小时', () => { + const [start, end] = yd_date_range(0, 0, 0, 1); + expect(end.hour()).toBe((start.hour() + 1) % 24); + }); + + test('增加一分钟', () => { + const [start, end] = yd_date_range(0, 0, 0, 0, 1); + expect(end.minute()).toBe((start.minute() + 1) % 60); + }); + + test('增加一秒钟', () => { + const [start, end] = yd_date_range(0, 0, 0, 0, 0, 1); + expect(end.second()).toBe((start.second() + 1) % 60); + }); + + test('如果目标日期在当前日期之前,返回的顺序应调整', () => { + const [start, end] = yd_date_range(-1); // 减少一年 + expect(start.isBefore(end)).toBe(true); + }); +}); From 234afc2aa6da14d953652097bb61d0216ceebfed Mon Sep 17 00:00:00 2001 From: bianxuerui <407955981@qq.com> Date: Mon, 5 Aug 2024 16:01:01 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E5=88=A0=E9=99=A4dayjs=E6=94=B9?= =?UTF-8?q?=E7=94=A8date-fns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/datetime/computeAge.js | 15 ++++++++++----- lib/datetime/computeAge.test.js | 10 +++++----- lib/datetime/dateRange.js | 11 +++++------ lib/datetime/dateRange.test.js | 21 +++++++++++---------- package.json | 3 +-- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/datetime/computeAge.js b/lib/datetime/computeAge.js index 784cc1b..887d22b 100644 --- a/lib/datetime/computeAge.js +++ b/lib/datetime/computeAge.js @@ -1,4 +1,4 @@ -import dayjs from 'dayjs'; +import { parseISO, isValid, differenceInYears, isBefore } from 'date-fns'; /** * 计算年龄 @@ -12,14 +12,19 @@ import dayjs from 'dayjs'; */ const computeAge = (birthday) => { if (birthday) { - const _birthday = dayjs(birthday); + const _birthday = parseISO(birthday); - if (!_birthday.isValid()) { + if (!isValid(_birthday)) { return 0; } - const cur = dayjs(); - const age = cur.get('year') - _birthday.get('year') - (cur.get('month') < _birthday.get('month') || (cur.get('month') == _birthday.get('month') && cur.get('date') < _birthday.get('date')) ? 1 : 0); + const cur = new Date(); + let age = differenceInYears(cur, _birthday); + + if (isBefore(cur, new Date(cur.getFullYear(), _birthday.getMonth(), _birthday.getDate()))) { + age--; + } + return age; } return 0; diff --git a/lib/datetime/computeAge.test.js b/lib/datetime/computeAge.test.js index 124ea90..623f7c3 100644 --- a/lib/datetime/computeAge.test.js +++ b/lib/datetime/computeAge.test.js @@ -1,24 +1,24 @@ import { describe, test, expect } from 'vitest'; -import dayjs from 'dayjs'; +import { format, parseISO, getYear, subMonths } from 'date-fns'; import dy_compute_age from './computeAge'; // 确保路径正确 describe('dy_compute_age', () => { test('正确计算年龄', () => { const birthday = '1990-01-01'; // 例如,出生日期为 1990 年 1 月 1 日 - const expectedAge = dayjs().year() - 1990; // 计算预期的年龄 + const expectedAge = new Date().getFullYear() - 1990; // 计算预期的年龄 const age = dy_compute_age(birthday); expect(age).toBe(expectedAge); }); test('计算同一天出生的年龄应为 0', () => { - const today = dayjs().format('YYYY-MM-DD'); + const today = format(new Date(), 'yyyy-MM-dd'); const age = dy_compute_age(today); expect(age).toBe(0); }); test('边界情况:出生日期在今年的某一天', () => { - const birthday = dayjs().subtract(5, 'month').format('YYYY-MM-DD'); // 例如,5 个月前 - const expectedAge = dayjs().year() - dayjs(birthday).year(); + const birthday = format(subMonths(new Date(), 5), 'yyyy-MM-dd'); // 例如,5 个月前 + const expectedAge = getYear(new Date()) - getYear(parseISO(birthday)); const age = dy_compute_age(birthday); expect(age).toBe(expectedAge); }); diff --git a/lib/datetime/dateRange.js b/lib/datetime/dateRange.js index 21208bc..bdeb9ab 100644 --- a/lib/datetime/dateRange.js +++ b/lib/datetime/dateRange.js @@ -1,8 +1,7 @@ -import dayjs from 'dayjs'; +import { add, isBefore } from 'date-fns'; /** * 返回一个时间范围 - * @author 卞雪瑞 * @category 时间操作 * @alias yd_date_range * @param {number} year - 需要增加的年数,默认为0 @@ -14,14 +13,14 @@ import dayjs from 'dayjs'; * @summary 生成一个时间范围数组,该数组包含当前时间和目标时间 * 如果目标时间在当前时间之后,则返回 [当前时间, 目标时间] 的数组 * 如果目标时间在当前时间之前或相同,则返回 [目标时间, 当前时间] 的数组 - * @returns {Array} 返回一个包含两个dayjs对象的数组,分别代表开始时间和结束时间 + * @returns {Array} 返回一个包含两个Date对象的数组,分别代表开始时间和结束时间 */ const dateRange = (year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0) => { - const now = dayjs(); - const targetDate = now.add(year, 'year').add(month, 'month').add(day, 'day').add(hour, 'hour').add(minute, 'minute').add(second, 'second'); + const now = new Date(); + const targetDate = add(now, { years: year, months: month, days: day, hours: hour, minutes: minute, seconds: second }); - if (now.isBefore(targetDate)) { + if (isBefore(now, targetDate)) { return [now, targetDate]; } return [targetDate, now]; diff --git a/lib/datetime/dateRange.test.js b/lib/datetime/dateRange.test.js index 87a43af..4f71744 100644 --- a/lib/datetime/dateRange.test.js +++ b/lib/datetime/dateRange.test.js @@ -1,46 +1,47 @@ import { describe, test, expect } from 'vitest'; -import dayjs from 'dayjs'; +import { add, isEqual, isBefore, startOfSecond } from 'date-fns'; import yd_date_range from './dateRange'; describe('yd_date_range', () => { test('默认情况下返回当前时间范围', () => { const [start, end] = yd_date_range(); - expect(start.isSame(dayjs(), 'second')).toBe(true); - expect(end.isSame(dayjs(), 'second')).toBe(true); + const now = new Date(); + expect(isEqual(startOfSecond(start), startOfSecond(now))).toBe(true); + expect(isEqual(startOfSecond(end), startOfSecond(now))).toBe(true); }); test('增加一年', () => { const [start, end] = yd_date_range(1); - expect(end.year()).toBe(start.year() + 1); + expect(end.getFullYear()).toBe(start.getFullYear() + 1); }); test('增加一个月', () => { const [start, end] = yd_date_range(0, 1); - expect(end.month()).toBe((start.month() + 1) % 12); + expect((end.getMonth() + 12) % 12).toBe((start.getMonth() + 1) % 12); }); test('增加一天', () => { const [start, end] = yd_date_range(0, 0, 1); - expect(end.date()).toBe(start.date() + 1); + expect(end.getDate()).toBe(start.getDate() + 1); }); test('增加一小时', () => { const [start, end] = yd_date_range(0, 0, 0, 1); - expect(end.hour()).toBe((start.hour() + 1) % 24); + expect((end.getHours() + 24) % 24).toBe((start.getHours() + 1) % 24); }); test('增加一分钟', () => { const [start, end] = yd_date_range(0, 0, 0, 0, 1); - expect(end.minute()).toBe((start.minute() + 1) % 60); + expect((end.getMinutes() + 60) % 60).toBe((start.getMinutes() + 1) % 60); }); test('增加一秒钟', () => { const [start, end] = yd_date_range(0, 0, 0, 0, 0, 1); - expect(end.second()).toBe((start.second() + 1) % 60); + expect((end.getSeconds() + 60) % 60).toBe((start.getSeconds() + 1) % 60); }); test('如果目标日期在当前日期之前,返回的顺序应调整', () => { const [start, end] = yd_date_range(-1); // 减少一年 - expect(start.isBefore(end)).toBe(true); + expect(isBefore(start, end)).toBe(true); }); }); diff --git a/package.json b/package.json index be5bb49..2c8b858 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,7 @@ "inspect": "eslint --inspect-config" }, "dependencies": { - "date-fns": "^3.6.0", - "dayjs": "^1.11.12" + "date-fns": "^3.6.0" }, "devDependencies": { "@eslint/config-inspector": "^0.5.2",