{"id":"dc191eb798b33704","repo":"date-fns/date-fns","slug":"the-format-string-mustn-t-contain-token-and-a","errorCode":null,"errorMessage":"The format string mustn't contain `${token}` and any other token at the same time","messagePattern":"The format string mustn't contain `(.+?)` and any other token at the same time","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"pkgs/core/src/parse/index.ts","lineNumber":448,"sourceCode":"    }\n\n    const firstCharacter = token[0];\n    const parser = parsers[firstCharacter];\n    if (parser) {\n      const { incompatibleTokens } = parser;\n      if (Array.isArray(incompatibleTokens)) {\n        const incompatibleToken = usedTokens.find(\n          (usedToken) =>\n            incompatibleTokens.includes(usedToken.token) ||\n            usedToken.token === firstCharacter,\n        );\n        if (incompatibleToken) {\n          throw new RangeError(\n            `The format string mustn't contain \\`${incompatibleToken.fullToken}\\` and \\`${token}\\` at the same time`,\n          );\n        }\n      } else if (parser.incompatibleTokens === \"*\" && usedTokens.length > 0) {\n        throw new RangeError(\n          `The format string mustn't contain \\`${token}\\` and any other token at the same time`,\n        );\n      }\n\n      usedTokens.push({ token: firstCharacter, fullToken: token });\n\n      const parseResult = parser.run(\n        dateStr,\n        token,\n        locale.match,\n        subFnOptions,\n      );\n\n      if (!parseResult) {\n        return invalidDate();\n      }\n\n      setters.push(parseResult.setter);","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/date-fns/date-fns/blob/4098115cf705e3af7f663d8e5b0686e39a9f478a/pkgs/core/src/parse/index.ts#L430-L466","documentation":"parse() at parse/index.ts:447-450 handles parsers whose incompatibleTokens === '*', meaning they cannot coexist with ANY other token in the same format string (e.g. the localized 'P'/'p' long-form tokens, or the standalone 'p' time-only token). If usedTokens already has anything when such a token is reached, parse throws RangeError naming the all-incompatible token.","triggerScenarios":"parse(s, 'P yyyy', ...) mixes the localized long-date 'P' with a calendar-year 'y'; parse(s, 'pp HH:mm', ...) mixes two time-only patterns; any format that combines a '*'-incompatible token with another token.","commonSituations":"Concatenating a localized 'P' pattern with extra tokens to add a time component; copy-pasting a locale-aware format string and tacking on more tokens; mismatched expectations from Moment.js migration.","solutions":["Use a fully explicit format string ('yyyy-MM-dd HH:mm') instead of mixing 'P' or 'p' with other tokens.","If you need localized date+time, use a single locale-aware pattern (the locale's formatLong.date + ' ' + formatLong.time) or call parse twice.","Check the parser's incompatibleTokens value (look for '*' in pkgs/core/src/_lib/parse) when in doubt."],"exampleFix":"// before\nparse(input, 'P p', referenceDate)\n// after\nparse(input, 'yyyy-MM-dd HH:mm:ss', referenceDate)","handlingStrategy":"validation","validationCode":"// '*'-incompatible tokens cannot share a format with anything else.\nconst STAR_INCOMPATIBLE = new Set(['P','p','Q','q'])\nfunction assertNoMixedStarToken(fmt: string) {\n  const re = /(\\w)\\1*|''|'(''|[^'])+('|$)|./g\n  const toks = (fmt.match(re) ?? []).filter(t => /[a-zA-Z]/.test(t[0]))\n  const star = toks.find(t => STAR_INCOMPATIBLE.has(t[0]))\n  if (star && toks.length > 1) throw new Error(`Token ${star} cannot mix with others`)\n}","typeGuard":null,"tryCatchPattern":"try { return parse(s, fmt, ref) }\ncatch (e) { if (e instanceof RangeError && /and any other token at the same time/.test(e.message)) return new Date(NaN); throw e }","preventionTips":["Use one localized token at a time, or none.","Switch to an explicit format when you need date+time.","Document '*'-incompatible tokens in your parse helper."],"tags":["parse","unicode-tokens","incompatible-tokens","localized","range-error"],"analyzedSha":"4098115cf705e3af7f663d8e5b0686e39a9f478a","analyzedAt":"2026-08-03T20:20:32.267Z","schemaVersion":2}