Skip to content

Commit beb80fc

Browse files
committed
fix: Retain UTC mode when constructing a new instance from another instance in UTC mode
1 parent 16581d7 commit beb80fc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ const parseLocale = (preset, object, isLocal) => {
2929

3030
const dayjs = (date, c) => {
3131
if (isDayjs(date)) {
32-
return date.clone()
32+
if (c) {
33+
date = date.toDate()
34+
} else {
35+
return date.clone()
36+
}
3337
}
3438
const cfg = c || {}
3539
cfg.date = date

test/parse.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ it('Does not apply the UTC mode by default', () => {
7979
expect(instance.minute()).toEqual(34)
8080
})
8181

82+
it('Creates an UTC instance from another instance', () => {
83+
const source = dayjs('2018-09-06')
84+
const instance = dayjs(source, { utc: true })
85+
expect(instance.$u).toBeTruthy()
86+
expect(instance.hour()).toEqual(source.toDate().getUTCHours())
87+
expect(instance.minute()).toEqual(source.toDate().getUTCMinutes())
88+
})
89+
90+
it('Creating a new instance from another instance retains the UTC mode', () => {
91+
const source = dayjs('2018-09-06', { utc: true })
92+
const instance = dayjs(source)
93+
expect(instance.$u).toBeTruthy()
94+
expect(instance.hour()).toEqual(source.hour())
95+
expect(instance.minute()).toEqual(source.minute())
96+
})
97+
8298
it('Clone not affect each other', () => {
8399
const base = dayjs(20170101)
84100
const year = base.year()
@@ -93,3 +109,9 @@ it('Clone with same value', () => {
93109
const another = newBase.clone()
94110
expect(newBase.toString()).toBe(another.toString())
95111
})
112+
113+
it('Clone retains the UTC mode', () => {
114+
const instance = dayjs('2018-09-06', { utc: true })
115+
const another = instance.clone()
116+
expect(another.$u).toBeTruthy()
117+
})

0 commit comments

Comments
 (0)