Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update utc plugin to support keepLocalTime .utc(true) #1080

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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