Skip to content

Commit

Permalink
fix: Duration add/subtract iamkun#2336
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-ulaev committed Jun 24, 2023
1 parent b6a5a44 commit 705d9b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,13 @@ class Duration {
}

const manipulateDuration = (date, duration, k) =>
date.add(duration.years() * k, 'year')
.add(duration.months() * k, 'month')
.add(duration.days() * k, 'day')
.add(duration.hours() * k, 'hour')
.add(duration.minutes() * k, 'minute')
.add(duration.seconds() * k, 'second')
.add(duration.milliseconds() * k, 'millisecond')
date.add(duration.years() * k, 'y')
.add(duration.months() * k, 'M')
.add(duration.days() * k, 'd')
.add(duration.hours() * k, 'h')
.add(duration.minutes() * k, 'm')
.add(duration.seconds() * k, 's')
.add(duration.milliseconds() * k, 'ms')

export default (option, Dayjs, dayjs) => {
$d = dayjs
Expand Down
16 changes: 6 additions & 10 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,10 @@ test('Add duration', () => {
const a = dayjs('2020-10-01')
const days = dayjs.duration(2, 'days')
expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03')
})

test('Add duration month', () => {
const a = dayjs('2023-02-01')
const days = dayjs.duration(1, 'month')
expect(a.add(days).format('YYYY-MM-DD')).toBe('2023-03-01')
const b = dayjs('2023-02-01 00:00:00')
const p = dayjs.duration('P1Y1M1DT1H1M1S')
expect(b.add(p).format('YYYY-MM-DD HH:mm:ss')).toBe('2024-03-02 01:01:01')
})

describe('Subtract', () => {
Expand All @@ -200,12 +198,10 @@ test('Subtract duration', () => {
const a = dayjs('2020-10-20')
const days = dayjs.duration(2, 'days')
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18')
})

test('Subtract duration month', () => {
const a = dayjs('2023-03-01')
const days = dayjs.duration(1, 'month')
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2023-02-01')
const b = dayjs('2023-03-02 02:02:02')
const p = dayjs.duration('P1Y1M1DT1H1M1S')
expect(b.subtract(p).format('YYYY-MM-DD HH:mm:ss')).toBe('2022-02-01 01:01:01')
})

describe('Seconds', () => {
Expand Down

0 comments on commit 705d9b3

Please sign in to comment.