Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add month or year changes hour when utc offset is used #957

Merged
merged 2 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class Dayjs {
const date = this.clone().set(C.DATE, 1)
date.$d[name](arg)
date.init()
this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).toDate()
this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).$d
} else if (name) this.$d[name](arg)

this.init()
Expand Down
26 changes: 25 additions & 1 deletion test/plugin/utc-utcOffset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ it('clone', () => {

it('immutable', () => {
const d = dayjs()
const du = d.utcOffset(9)
const du = d.utcOffset(d.utcOffset() + 1)
expect(d.utcOffset()).not.toBe(du.utcOffset())
expect(d.format()).not.toBe(du.format())
})
Expand Down Expand Up @@ -76,6 +76,30 @@ test('change hours when changing the utc offset in UTC mode', () => {
expect(d.utcOffset(-1380).format('HH:mm')).toBe('07:31')
})

test('correctly set and add hours in offset mode', () => {
const d10 = dayjs('2000-01-30T06:31:00+10:00').utcOffset(10)
const dm8 = dayjs('2000-01-30T06:31:00-08:00').utcOffset(-8)

expect(d10.hour(5).hour()).toBe(5)
expect(d10.hour(5).add(1, 'hour').hour()).toBe(6)
expect(d10.hour(5).add(-10, 'hour').hour()).toBe(19)

expect(dm8.hour(5).hour()).toBe(5)
expect(dm8.hour(5).add(1, 'hour').hour()).toBe(6)
expect(dm8.hour(5).add(-10, 'hour').hour()).toBe(19)
})

test('keep hours when adding month in offset mode', () => {
const d10 = dayjs('2000-01-30T06:31:00+10:00').utcOffset(10)
const dm8 = dayjs('2000-01-30T06:31:00-08:00').utcOffset(-8)

expect(d10.add(1, 'month').hour()).toBe(6)
expect(dm8.add(1, 'month').hour()).toBe(6)

expect(d10.add(-2, 'month').hour()).toBe(6)
expect(dm8.add(-2, 'month').hour()).toBe(6)
})

test('utc costrustor', () => {
const d = new Date(2019, 8, 11, 0, 0, 0).getTime()
expect(moment(d).utc().utcOffset(480).valueOf())
Expand Down