Skip to content

Commit 5ce4e0d

Browse files
authored
fix: update utc plugin to support keepLocalTime .utc(true) (#1080)
1 parent a6ebcf2 commit 5ce4e0d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/plugin/utc/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ export default (option, Dayjs, dayjs) => {
77
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
88
}
99

10-
proto.utc = function () {
11-
return dayjs(this.toDate(), { locale: this.$L, utc: true })
10+
proto.utc = function (keepLocalTime) {
11+
const ins = dayjs(this.toDate(), { locale: this.$L, utc: true })
12+
if (keepLocalTime) {
13+
return ins.add(this.utcOffset(), MIN)
14+
}
15+
return ins
1216
}
1317

1418
proto.local = function () {

test/plugin/utc.test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import MockDate from 'mockdate'
22
import moment from 'moment'
33
import dayjs from '../../src'
4-
import utc from '../../src/plugin/utc'
54
import customParseFormat from '../../src/plugin/customParseFormat'
5+
import utc from '../../src/plugin/utc'
66

77
dayjs.extend(utc)
88

@@ -250,3 +250,19 @@ describe('Diff', () => {
250250
.toBe(moment.utc(d1).diff(d2, 'm'))
251251
})
252252
})
253+
254+
it('utc keepLocalTime', () => {
255+
const t = '2016-05-03 22:15:01'
256+
const d = dayjs(t).utc(true)
257+
const m = moment(t).utc(true)
258+
const fd = d.format()
259+
const dd = d.toDate()
260+
const vd = d.valueOf()
261+
const fm = m.format()
262+
const dm = m.toDate()
263+
const vm = m.valueOf()
264+
expect(fd).toEqual(fm)
265+
expect(fd).toEqual('2016-05-03T22:15:01Z')
266+
expect(dd).toEqual(dm)
267+
expect(vd).toEqual(vm)
268+
})

types/plugin/utc.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export = plugin
66
declare module 'dayjs' {
77
interface Dayjs {
88

9-
utc(): Dayjs
9+
utc(keepLocalTime?: boolean): Dayjs
1010

1111
local(): Dayjs
1212

0 commit comments

Comments
 (0)