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
8 changes: 8 additions & 0 deletions src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ export default (o, c, d) => {
// timestamp number || js Date || Day.js
return d(input).tz(timezone)
}

const hasExplicitOffset = /[Zz]|[+-]\d\d:?(\d\d)?$/.test(input)
Copy link
Owner

@iamkun iamkun Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const hasTimezoneInfo = /Z|[+-]\d{2}:?\d{2}$/.test(input)
if (hasTimezoneInfo && !parseFormat) {
return d(input).tz(timezone)
}
Can we fix it in this way?

  • Detect if the input string has timezone markers (Z or +HH:MM)
  • If yes & no custom format: Let dayjs parse it normally (which correctly handles UTC), then convert to target timezone
  • If no: Keep existing behavior (treat as local time in target timezone)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sounds good, will run a few tests

if (hasExplicitOffset) {
const utcDate = d.utc(input, parseFormat)
utcDate.$x.$timezone = timezone

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it this way doesn't set the offset or any of the time fields so the resulting object likely doesn't do what you think it does here. It's also not covered by test cases, your input is UTC (not an offset), toISOString is always UTC, so comparing it to moment will pass with any value for timezone.

return utcDate
}

const localTs = d.utc(input, parseFormat).valueOf()
const [targetTs, targetOffset] = fixOffset(localTs, previousOffset, timezone)
const ins = d(targetTs).utcOffset(targetOffset)
Expand Down
22 changes: 21 additions & 1 deletion test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import MockDate from 'mockdate'
import moment from 'moment-timezone'
import dayjs from '../../src'
import timezone from '../../src/plugin/timezone'
import customParseFormat from '../../src/plugin/customParseFormat'
import timezone from '../../src/plugin/timezone'
import utc from '../../src/plugin/utc'

dayjs.extend(utc)
Expand All @@ -22,6 +22,7 @@ const VAN = 'America/Vancouver'
const DEN = 'America/Denver'
const TOKYO = 'Asia/Tokyo'
const PARIS = 'Europe/Paris'
const MELBOURNE = 'Australia/Melbourne'

describe('Guess', () => {
it('return string', () => {
Expand Down Expand Up @@ -352,3 +353,22 @@ describe('UTC timezone', () => {
expect(dayjs2.format()).toBe(moment2.format())
})
})

describe('timezone: parsing UTC input when default timezone is set', () => {
beforeEach(() => {
dayjs.tz.setDefault(MELBOURNE)
moment.tz.setDefault(MELBOURNE)
})
afterEach(() => {
dayjs.tz.setDefault()
moment.tz.setDefault()
})

it('should honor Z (UTC) when parsing with tz()', () => {
const input = '2025-10-24T05:00:00Z'
const d = dayjs.tz(input, MELBOURNE)
const m = moment.tz(input, MELBOURNE)

expect(d.toISOString()).toBe(m.toISOString())
})
})