{"success":true,"name":"javascript-date-traps-months-start-at-zero-and-string-parsin","description":"Working in JavaScript and constructed or parsed dates come out wrong","category":"ai-brimind","path":"ai-brimind/javascript-date-traps-months-start-at-zero-and-string-parsin/SKILL.md","file":"SKILL.md","content":"---\nname: javascript-date-traps-months-start-at-zero-and-string-parsin\ndescription: \"Working in JavaScript and constructed or parsed dates come out wrong\"\n---\n\n# JavaScript Date Traps: Months Start at Zero and String Parsing Is a Lottery\n\n## When to use\nProject is JavaScript, and dates are built with numeric parts or parsed from strings — and results are off by a month, a day, or vary by machine.\n\n## Example request\nnew Date(2026, 7, 4) expected as July 4 → month 7 is August, months are 0-indexed; the classic off-by-one-month. new Date('04/07/2026') → parsed as April 7 in US style, or Invalid Date elsewhere; format lottery. new Date('2026-07-04') vs with a time → date-only strings parse as UTC midnight and can display as the prior day; hidden timezone shift. — each trusted Date to guess correctly.\n\n## Instructions\nAct as a mechanic who knows this particular machine's gauges are mislabeled and reads the manual, not the dial. JavaScript's Date carries historical traps that hit precisely at construction and parsing: months are 0-indexed (January is 0, December is 11) in both the constructor and getMonth — so always add or subtract 1 when converting to and from human month numbers, and comment it at the site; day-of-month, by contrast, is 1-based, making the asymmetry easy to forget. Never parse ambiguous or localized date strings with the Date constructor — slash formats and month names parse differently across engines and locales or yield Invalid Date; accept only the ISO format, or split the string and construct from numeric parts yourself. Know the ISO subtlety: a date-only ISO string is treated as UTC midnight while date-with-time is treated as local — mixing the two shifts days silently, per the calendar-date rules. After any parse, verify validity explicitly (an invalid Date is detectable via its NaN time value) instead of letting it flow onward. If the project already uses a date library, route all construction and parsing through it. Only proceed once no date in the change is born from a guessed string or an unadjusted month number.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #783\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}