Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
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.
Bad example
| 1 | const date = new Date(dateString); // 'heyoo' yields Invalid Date silently |
| 2 | return date.toLocaleDateString(); |
Explanation (EN)
An invalid timestamp can propagate into formatting without representing a usable date.
Objašnjenje (HR)
Good example
| 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)
Notes (EN)
Validate the accepted format and real calendar/instant semantics before constructing and formatting a date. Check Invalid Date and the intended timezone.
Exceptions / Tradeoffs (EN)
A valid getTime result alone does not prove the original text matched the required format or calendar day: some inputs normalize overflow or parse implementation-specific forms. Use strict parsing when the contract requires it.