Skip to content

fix(utc): handle short UTC offset format without minutes#3151

Open
CYRyo wants to merge 1 commit into
iamkun:devfrom
CYRyo:fix/utc-offset-no-minutes
Open

fix(utc): handle short UTC offset format without minutes#3151
CYRyo wants to merge 1 commit into
iamkun:devfrom
CYRyo:fix/utc-offset-no-minutes

Conversation

@CYRyo

@CYRyo CYRyo commented Jun 27, 2026

Copy link
Copy Markdown

Summary

utcOffset('+08') and utcOffset('-05') return NaN instead of the correct offset in minutes.

The validation regex REGEX_VALID_OFFSET_FORMAT correctly accepts short offsets like +08 (minutes are optional via (?::?\d\d)?), but the parsing regex REGEX_OFFSET_HOURS_MINUTES_FORMAT only captures ['+', '08']minutesOffset is undefined, causing (+8 * 60) + (+undefined) = NaN.

Reproduction

dayjs().utcOffset('+08')    // broken: internal offset becomes NaN
dayjs().utcOffset('+08:00') // works: 480
dayjs().utcOffset('-05')    // broken: NaN
dayjs().utcOffset('-05:00') // works: -300

Fix

Default minutesOffset to 0 when no minutes group is captured:

- const totalOffsetInMinutes = (+hoursOffset * 60) + (+minutesOffset)
+ const totalOffsetInMinutes = (+hoursOffset * 60) + (+(minutesOffset || 0))

offsetFromString returns NaN for offsets like "+08" or "-05" (without
minutes component). The regex REGEX_VALID_OFFSET_FORMAT correctly
accepts these formats, but the destructured minutesOffset is undefined
when no minutes group is captured, causing (+undefined * 60) + NaN.

Default minutesOffset to 0 when not captured.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant