Skip to content

Commit

Permalink
fix: Fix UTC plugin startOf, endOf bug
Browse files Browse the repository at this point in the history
fix #809, fix #808
  • Loading branch information
iamkun committed Apr 17, 2020
1 parent 62b092d commit 7ae636f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Dayjs {
const argumentStart = [0, 0, 0, 0]
const argumentEnd = [23, 59, 59, 999]
return Utils.w(this.toDate()[method].apply( // eslint-disable-line prefer-spread
this.toDate(),
this.toDate('s'),
(isStartOf ? argumentStart : argumentEnd).slice(slice)
), this)
}
Expand Down
8 changes: 8 additions & 0 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ export default (option, Dayjs, dayjs) => {
proto.toString = function () {
return this.toDate().toUTCString()
}

const oldToDate = proto.toDate
proto.toDate = function (type) {
if (type === 's' && this.$offset) {
return dayjs(this.format('YYYY-MM-DD HH:mm:ss:SSS')).toDate()
}
return oldToDate.call(this)
}
}
7 changes: 7 additions & 0 deletions test/plugin/utc-utcOffset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ test('utc startOf', () => {
.valueOf())
.toBe(dayjs(d).utc().utcOffset(480).endOf('day')
.valueOf())
const d2 = '2017-07-20T11:00:00+00:00'
const d2d = dayjs(d2).utcOffset(-12).startOf('day').valueOf()
const d2m = moment(d2).utcOffset(-12).startOf('day').valueOf()
expect(d2d)
.toBe(d2m)
expect(d2d)
.toBe(1500465600000)
})

0 comments on commit 7ae636f

Please sign in to comment.