{"record":{"id":"c3156781b2633dd4","repo":"can1357/oh-my-pi","slug":"invalid-time-value","errorCode":null,"errorMessage":"Invalid time value","messagePattern":"Invalid time value","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/dates.ts","lineNumber":94,"sourceCode":"\t\t\treturn pad(hours12);\n\t\tcase \"h\":\n\t\t\treturn String(hours12);\n\t\tcase \"mm\":\n\t\t\treturn pad(date.getMinutes());\n\t\tcase \"m\":\n\t\t\treturn String(date.getMinutes());\n\t\tcase \"ss\":\n\t\t\treturn pad(date.getSeconds());\n\t\tcase \"s\":\n\t\t\treturn String(date.getSeconds());\n\t\tcase \"a\":\n\t\t\treturn hours < 12 ? \"AM\" : \"PM\";\n\t}\n}\n\nfunction asDate(value: Date | number): Date {\n\tconst date = value instanceof Date ? new Date(value.getTime()) : new Date(value);\n\tif (Number.isNaN(date.getTime())) throw new RangeError(\"Invalid time value\");\n\treturn date;\n}\n\n/** Format a date with the supported date-fns v4 tokens and quoted literals. */\nexport function format(value: Date | number, pattern: string): string {\n\tconst date = asDate(value);\n\tlet result = \"\";\n\n\tfor (let index = 0; index < pattern.length; ) {\n\t\tif (pattern[index] === \"'\") {\n\t\t\tif (pattern[index + 1] === \"'\") {\n\t\t\t\tresult += \"'\";\n\t\t\t\tindex += 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tindex++;\n\t\t\twhile (index < pattern.length) {","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/dates.ts#L76-L112","documentation":"asDate (packages/utils/src/dates.ts) copies or constructs a Date and throws RangeError(\"Invalid time value\") when the resulting date's getTime() is NaN — i.e. the input was an invalid Date (e.g. new Date(NaN)) or an unparseable numeric timestamp. It guards the date-fns-compatible format() functions from producing \"Invalid Date\" strings.","triggerScenarios":"Calling format(), or any function routed through date()/asDate(), with a Date created from an invalid input (new Date('not-a-date')), NaN, Infinity, or a Date field deserialized as null then coerced.","commonSituations":"Formatting timestamps parsed from user input, JSON payloads where a date string was malformed, or clock values of 0/NaN from failed timers or DB reads.","solutions":["Validate the Date before formatting: check Number.isNaN(date.getTime()) and handle the invalid case.","Fix the source of the bad timestamp (malformed string, null, NaN) rather than formatting it.","Use Date.parse() or a strict parsing routine on strings before constructing the Date.","For numeric inputs, confirm the value is a finite epoch milliseconds number."],"exampleFix":"// before\nformat(new Date(input), 'yyyy-MM-dd'); // RangeError when input is garbage\n// after\nconst d = new Date(input);\nif (Number.isNaN(d.getTime())) {\n  return '—'; // or surface a validation error\n}\nformat(d, 'yyyy-MM-dd');","handlingStrategy":"validation","validationCode":"function isValidDate(v) {\n  const d = v instanceof Date ? v : new Date(v);\n  return !Number.isNaN(d.getTime());\n}","typeGuard":"function isRealDate(value: Date | number): value is Date {\n  const d = value instanceof Date ? value : new Date(value);\n  return !Number.isNaN(d.getTime());\n}","tryCatchPattern":"try {\n  return format(value, 'yyyy-MM-dd HH:mm');\n} catch (err) {\n  if (err instanceof RangeError && err.message === 'Invalid time value') {\n    return 'invalid date';\n  }\n  throw err;\n}","preventionTips":["Validate timestamps at parse boundaries (JSON input, user input) before constructing Dates.","Use strict date parsing for strings instead of relying on new Date(string).","Check Number.isNaN(d.getTime()) before formatting any Date from external data."],"tags":["dates","validation","range-error"],"backgroundTag":"invalid-date-value","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}