fix: add S and SS fractional-second tokens to format()#3145
Open
JSap0914 wants to merge 1 commit into
Open
Conversation
The base format() function only handled SSS (3-digit milliseconds).
Single S (hundreds digit) and SS (first two digits) were not matched
by REGEX_FORMAT, so dayjs().format('S') and dayjs().format('SS')
returned literal 'S'/'SS' instead of fractional-second values.
This also caused customParseFormat strict mode to reject valid dates
when the format string contained S or SS, because the strict-mode
roundtrip check (date != this.format(format)) would compare an actual
timestamp against a string with literal 'S'/'SS' characters.
Fix:
- Extend REGEX_FORMAT in constant.js from /SSS/ to /S{1,3}/ so that
S, SS, and SSS are all captured as format tokens.
- Add case 'S' and case 'SS' to the matches() switch in index.js:
S → Math.floor(ms / 100) (hundreds digit, 0–9)
SS → Math.floor(ms / 10) (first two digits, zero-padded 00–99)
Fixes: iamkun#1331
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
The base
format()function only handledSSS(3-digit milliseconds). TheS(hundreds digit) andSS(first two digits) tokens were absent fromREGEX_FORMAT, so they were never matched — instead they appeared as literal characters in the output:This also broke
customParseFormatstrict mode forS/SSformats. Strict mode re-formats the parsed date and compares it against the original input; becauseformat('S')returned a literal'S', the comparison always failed:Reported in #1331.
Fix
src/constant.js— extendREGEX_FORMATfrom/SSS/to/S{1,3}/so that single and doubleStokens are matched.src/index.js— add two new cases to thematches()switch insideformat():SMath.floor(ms / 100)'5'SSUtils.s(Math.floor(ms / 10), 2, '0')'52'SSSUtils.s(ms, 3, '0')(unchanged)'520'The token semantics match
customParseFormat's existing parse-side expressions (S×100,SS×10,SSS×1), so strict-mode roundtrips now succeed.Verification
Full suite: 779 tests pass, 0 failures.