diff --git a/package.json b/package.json index a8a682645..359cc2c53 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "types": "index.d.ts", "module": "dayjs.min.js", "scripts": { - "test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && jest", - "test-tz": "jest test/timezone.test --coverage=false", + "test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && TZ=America/Whitehorse npm run test-tz && npm run test-tz && jest", + "test-tz": "date && jest test/timezone.test --coverage=false", "lint": "./node_modules/.bin/eslint src/* test/* build/*", "prettier": "prettier --write \"docs/**/*.md\"", "babel": "cross-env BABEL_ENV=build babel src --out-dir esm --copy-files && node build/esm", diff --git a/src/index.js b/src/index.js index ccd0a95ec..3e0b5e141 100644 --- a/src/index.js +++ b/src/index.js @@ -242,9 +242,8 @@ class Dayjs { number = Number(number) // eslint-disable-line no-param-reassign const unit = Utils.p(units) const instanceFactorySet = (n) => { - const date = new Date(this.$d) - date.setDate(date.getDate() + Math.round(n * number)) - return Utils.w(date, this) + const d = dayjs(this) + return Utils.w(d.date(d.date() + Math.round(n * number)), this) } if (unit === C.M) { return this.set(C.M, this.$M + number) diff --git a/test/display.test.js b/test/display.test.js index 764a5d0ea..145e068fc 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -98,6 +98,12 @@ it('Format Minute m mm', () => { }) it('Format Second s ss SSS', () => { + // Todo: debug CI error + console.log(Date.now()) // eslint-disable-line no-console + console.log((new Date()).toString()) // eslint-disable-line no-console + console.log((new Date()).toLocaleString()) // eslint-disable-line no-console + console.log((new Date()).getTimezoneOffset()) // eslint-disable-line no-console + // debug expect(dayjs().format('s')).toBe(moment().format('s')) expect(dayjs().format('ss')).toBe(moment().format('ss')) expect(dayjs().format('SSS')).toBe(moment().format('SSS')) diff --git a/test/timezone.test.js b/test/timezone.test.js index 59410ad43..b2366f0d4 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -1,6 +1,9 @@ import moment from 'moment' import MockDate from 'mockdate' import dayjs from '../src' +import utc from '../src/plugin/utc' + +dayjs.extend(utc) beforeEach(() => { MockDate.set(new Date()) @@ -36,3 +39,18 @@ it('Diff (DST)', () => { expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true)) }) }) + +it('UTC add day in DST', () => { + const testDate = '2019-03-10' + const dayTest = dayjs(testDate) + .utc() + .startOf('day') + const momentTest = moment(testDate) + .utc() + .startOf('day') + expect(dayTest.add(1, 'day').format()) + .toBe(momentTest.clone().add(1, 'day').format()) + expect(dayTest.add(2, 'day').format()) + .toBe(momentTest.clone().add(2, 'day').format()) +}) +