Skip to content

Commit 5f446ed

Browse files
authored
fix: Fix Timezone plugin to preserve milliseconds while changing timezone (#1003)
* fix: fix Timezone plugin to preserve milliseconds while changing timezone fix #1002
1 parent 6e5ffce commit 5f446ed

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/plugin/timezone/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const typeToPos = {
77
second: 5
88
}
99

10+
const ms = 'ms'
11+
1012
export default (o, c, d) => {
1113
const localUtcOffset = d().utcOffset()
1214
const tzOffset = (timestamp, timezone) => {
@@ -55,7 +57,7 @@ export default (o, c, d) => {
5557
proto.tz = function (timezone) {
5658
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone })
5759
const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60)
58-
return d(target).utcOffset(localUtcOffset - diff, true)
60+
return d(target).utcOffset(localUtcOffset - diff, true).$set(ms, this.$ms)
5961
}
6062
d.tz = function (input, timezone) {
6163
const previousOffset = tzOffset(+d(), timezone)

test/plugin/timezone.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ describe('Parse', () => {
5656
expect(newYork.tz('America/Los_Angeles').format()).toBe('2014-06-01T09:00:00-07:00')
5757
expect(newYork.tz('Europe/London').format()).toBe('2014-06-01T17:00:00+01:00')
5858
})
59+
60+
it('preserve milliseconds', () => {
61+
const d = dayjs(1596735327399)
62+
const oldMs = d.millisecond()
63+
const dTz = d.tz('America/New_York')
64+
const newMs = dTz.millisecond()
65+
expect(oldMs).toEqual(newMs)
66+
})
5967
})
6068

6169
describe('Convert', () => {

0 commit comments

Comments
 (0)