From 4141084ba96d35cadcda3f1e661bf1d0f6c8e4de Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 17 Apr 2020 16:12:09 +0800 Subject: [PATCH] fix: Fix UTC plugin startOf, endOf bug (#872) fix #809, fix #808 --- src/index.js | 2 +- src/plugin/utc/index.js | 8 ++++++++ test/plugin/utc-utcOffset.test.js | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 8d0863559..f71f97f79 100644 --- a/src/index.js +++ b/src/index.js @@ -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) } diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 70223ce21..606a831a2 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -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) + } } diff --git a/test/plugin/utc-utcOffset.test.js b/test/plugin/utc-utcOffset.test.js index 0060113b3..ae11ef574 100644 --- a/test/plugin/utc-utcOffset.test.js +++ b/test/plugin/utc-utcOffset.test.js @@ -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) })