Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
fullstack ruleP1universalStack: javascript
datesvalidationrobustness
Validate a date string before relying on the parsed Date
TypeScript won't catch an invalid date string passed to new Date(); validate it in the consuming code.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetypescript
| 1 | const date = new Date(dateString); // 'heyoo' yields Invalid Date silently |
| 2 | return date.toLocaleDateString(); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | const date = new Date(dateString); |
| 2 | if (Number.isNaN(date.getTime())) { |
| 3 | return null; |
| 4 | } |
| 5 | return date.toLocaleDateString(); |
Explanation (EN)
Objašnjenje (HR)