Skip to content
Open
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
12 changes: 10 additions & 2 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ const getDateTimeFormat = (timezone, options = {}) => {
return dtf
}

function parseParts(parts) {
const objParts = parts.reduce((obj, part) => ({
...obj,
[part.type]: part.value,
}), {})
return new Date(objParts.year, objParts.month - 1, objParts.day, objParts.hour, objParts.minute, objParts.second)
}

export default (o, c, d) => {
let defaultTimezone

Expand Down Expand Up @@ -95,8 +103,8 @@ export default (o, c, d) => {
proto.tz = function (timezone = defaultTimezone, keepLocalTime) {
const oldOffset = this.utcOffset()
const date = this.toDate()
const target = date.toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((date - new Date(target)) / 1000 / 60)
const target = parseParts(getDateTimeFormat(timezone).formatToParts(date))
const diff = Math.round((date - target) / 1000 / 60)
let ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
if (keepLocalTime) {
Expand Down
2 changes: 1 addition & 1 deletion test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('Get offsetName', () => {
})
})

describe('CustomPraseFormat', () => {
describe('CustomParseFormat', () => {
const result = 1602786600
it('normal', () => {
expect(dayjs.tz('2020/10/15 12:30', DEN).unix()).toBe(result)
Expand Down