diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 10ff1ec9a..f1473570b 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -114,7 +114,7 @@ export default (option, Dayjs, dayjs) => { proto.valueOf = function () { const addedOffset = !this.$utils().u(this.$offset) - ? this.$offset + (this.$x.$localOffset || (new Date()).getTimezoneOffset()) : 0 + ? this.$offset + (this.$x.$localOffset || this.$d.getTimezoneOffset()) : 0 return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE) } diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index dfcebf47e..2b28cc309 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -21,6 +21,7 @@ const NY = 'America/New_York' const VAN = 'America/Vancouver' const DEN = 'America/Denver' const TOKYO = 'Asia/Tokyo' +const PARIS = 'Europe/Paris' describe('Guess', () => { it('return string', () => { @@ -212,6 +213,18 @@ describe('DST, a time that never existed Fall Back', () => { }) }) +it('DST valueOf', () => { + const day1 = '2021-11-17T09:45:00.000Z' + const d1 = dayjs.utc(day1).tz(PARIS) + const m1 = moment.tz(day1, PARIS) + expect(d1.valueOf()).toBe(m1.valueOf()) + + const day2 = '2021-05-17T09:45:00.000Z' + const d2 = dayjs.utc(day2).tz(PARIS) + const m2 = moment.tz(day2, PARIS) + expect(d2.valueOf()).toBe(m2.valueOf()) +}) + describe('set Default', () => { it('default timezone', () => { const dateStr = '2014-06-01 12:00' diff --git a/test/timezone.test.js b/test/timezone.test.js index 58e83d225..42cab8934 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -57,12 +57,20 @@ it('UTC add day in DST', () => { }) it('UTC and utcOffset', () => { - const test1 = 1331449199000 // 2012/3/11 14:59:59 - expect(moment(test1).utcOffset(-300).format()) - .toBe(dayjs(test1).utcOffset(-300).format()) + const test1 = 1331449199000 // 2012/3/11 06:59:59 GMT+0000 + expect(dayjs(test1).utcOffset(-300).format()) + .toBe(moment(test1).utcOffset(-300).format()) const test2 = '2000-01-01T06:31:00Z' - expect(moment.utc(test2).utcOffset(-60).format()) - .toBe(dayjs.utc(test2).utcOffset(-60).format()) + expect(dayjs.utc(test2).utcOffset(-60).format()) + .toBe(moment.utc(test2).utcOffset(-60).format()) + + // across DST, copied from utc.test.js#get utc offset with a number value + const time = '2021-02-28 19:40:10' + const hoursOffset = -8 + const daysJS = dayjs(time).utc().utcOffset(hoursOffset * 60, true) + const momentJS = moment(time).utc(true).utcOffset(hoursOffset, true) + + expect(daysJS.toISOString()).toEqual(momentJS.toISOString()) }) it('UTC diff in DST', () => {