Skip to content

Commit 4141084

Browse files
authored
fix: Fix UTC plugin startOf, endOf bug (#872)
fix #809, fix #808
1 parent 3cea04d commit 4141084

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class Dayjs {
172172
const argumentStart = [0, 0, 0, 0]
173173
const argumentEnd = [23, 59, 59, 999]
174174
return Utils.w(this.toDate()[method].apply( // eslint-disable-line prefer-spread
175-
this.toDate(),
175+
this.toDate('s'),
176176
(isStartOf ? argumentStart : argumentEnd).slice(slice)
177177
), this)
178178
}

src/plugin/utc/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,12 @@ export default (option, Dayjs, dayjs) => {
9191
proto.toString = function () {
9292
return this.toDate().toUTCString()
9393
}
94+
95+
const oldToDate = proto.toDate
96+
proto.toDate = function (type) {
97+
if (type === 's' && this.$offset) {
98+
return dayjs(this.format('YYYY-MM-DD HH:mm:ss:SSS')).toDate()
99+
}
100+
return oldToDate.call(this)
101+
}
94102
}

test/plugin/utc-utcOffset.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,11 @@ test('utc startOf', () => {
100100
.valueOf())
101101
.toBe(dayjs(d).utc().utcOffset(480).endOf('day')
102102
.valueOf())
103+
const d2 = '2017-07-20T11:00:00+00:00'
104+
const d2d = dayjs(d2).utcOffset(-12).startOf('day').valueOf()
105+
const d2m = moment(d2).utcOffset(-12).startOf('day').valueOf()
106+
expect(d2d)
107+
.toBe(d2m)
108+
expect(d2d)
109+
.toBe(1500465600000)
103110
})

0 commit comments

Comments
 (0)