diff --git a/src/index.js b/src/index.js index 5e20a071d..8dd2f57a9 100644 --- a/src/index.js +++ b/src/index.js @@ -29,7 +29,11 @@ const parseLocale = (preset, object, isLocal) => { const dayjs = (date, c) => { if (isDayjs(date)) { - return date.clone() + if (c) { + date = date.toDate() + } else { + return date.clone() + } } const cfg = c || {} cfg.date = date diff --git a/test/parse.test.js b/test/parse.test.js index 6b186fc66..a423bfefd 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -79,6 +79,22 @@ it('Does not apply the UTC mode by default', () => { expect(instance.minute()).toEqual(34) }) +it('Creates an UTC instance from another instance', () => { + const source = dayjs('2018-09-06') + const instance = dayjs(source, { utc: true }) + expect(instance.$u).toBeTruthy() + expect(instance.hour()).toEqual(source.toDate().getUTCHours()) + expect(instance.minute()).toEqual(source.toDate().getUTCMinutes()) +}) + +it('Creating a new instance from another instance retains the UTC mode', () => { + const source = dayjs('2018-09-06', { utc: true }) + const instance = dayjs(source) + expect(instance.$u).toBeTruthy() + expect(instance.hour()).toEqual(source.hour()) + expect(instance.minute()).toEqual(source.minute()) +}) + it('Clone not affect each other', () => { const base = dayjs(20170101) const year = base.year() @@ -93,3 +109,9 @@ it('Clone with same value', () => { const another = newBase.clone() expect(newBase.toString()).toBe(another.toString()) }) + +it('Clone retains the UTC mode', () => { + const instance = dayjs('2018-09-06', { utc: true }) + const another = instance.clone() + expect(another.$u).toBeTruthy() +})