Skip to content

Commit

Permalink
fix: update utc plugin to support keepLocalTime .utc(true) (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Sep 28, 2020
1 parent a6ebcf2 commit 5ce4e0d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ export default (option, Dayjs, dayjs) => {
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}

proto.utc = function () {
return dayjs(this.toDate(), { locale: this.$L, utc: true })
proto.utc = function (keepLocalTime) {
const ins = dayjs(this.toDate(), { locale: this.$L, utc: true })
if (keepLocalTime) {
return ins.add(this.utcOffset(), MIN)
}
return ins
}

proto.local = function () {
Expand Down
18 changes: 17 additions & 1 deletion test/plugin/utc.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import utc from '../../src/plugin/utc'
import customParseFormat from '../../src/plugin/customParseFormat'
import utc from '../../src/plugin/utc'

dayjs.extend(utc)

Expand Down Expand Up @@ -250,3 +250,19 @@ describe('Diff', () => {
.toBe(moment.utc(d1).diff(d2, 'm'))
})
})

it('utc keepLocalTime', () => {
const t = '2016-05-03 22:15:01'
const d = dayjs(t).utc(true)
const m = moment(t).utc(true)
const fd = d.format()
const dd = d.toDate()
const vd = d.valueOf()
const fm = m.format()
const dm = m.toDate()
const vm = m.valueOf()
expect(fd).toEqual(fm)
expect(fd).toEqual('2016-05-03T22:15:01Z')
expect(dd).toEqual(dm)
expect(vd).toEqual(vm)
})
2 changes: 1 addition & 1 deletion types/plugin/utc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export = plugin
declare module 'dayjs' {
interface Dayjs {

utc(): Dayjs
utc(keepLocalTime?: boolean): Dayjs

local(): Dayjs

Expand Down

0 comments on commit 5ce4e0d

Please sign in to comment.