Skip to content

fix(js/ts): reject JS-permissive date strings that .NET TryParse rejects#4588

Draft
fable-repo-assist[bot] wants to merge 2 commits intomainfrom
repo-assist/fix-datetime-tryparse-permissive-3858-7e4e6589f82de55e
Draft

fix(js/ts): reject JS-permissive date strings that .NET TryParse rejects#4588
fable-repo-assist[bot] wants to merge 2 commits intomainfrom
repo-assist/fix-datetime-tryparse-permissive-3858-7e4e6589f82de55e

Conversation

@fable-repo-assist
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Fixes DateTime.TryParse accepting strings in JavaScript that .NET correctly rejects.

Closes #3858

Root Cause

V8's Date constructor is overly permissive — it accepts strings like "ABC 6" by treating "ABC" as a timezone abbreviation and "6" as a year/month. .NET's DateTime.TryParse rejects such strings because they don't match any recognised date format pattern.

In parseRaw(), the code calls new Date(input) first and only falls back to a strict regex when isNaN(date.getTime()). Since V8 successfully parses "ABC 6", the guard never fires.

Fix

Add a pre-validation check at the top of parseRaw() that requires the string to start with either a digit or a recognised month-name prefix (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec — case-insensitive):

if (!/^\s*(?:\d|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i.test(input)) {
    fail();
}

This lets valid formats through:

  • Numeric dates: "9/10/2014", "2024-01-15", "2024-01-15T12:00:00Z"
  • Month-name dates: "January 15, 2024", "Dec 25 2024"
  • Time-only: "12:00:00"

And rejects V8-specific quirks:

  • "ABC 6" → fails (starts with A, not a digit or month name) ✓
  • "XYZ 2024" → fails ✓
  • "foo" → still fails (already tested) ✓

Changes

  • src/fable-library-ts/Date.ts: add pre-validation in parseRaw()
  • tests/Js/Main/DateTimeTests.fs: add regression test for "ABC 6" and "XYZ 2024"

Trade-offs

The regex is simple and only checks the start of the string. It does not attempt to fully validate the date format (that remains the job of new Date() and the fallback regex). This minimises the risk of breaking existing valid inputs while fixing the specific V8 over-acceptance bug.

Generated by 🌈 Repo Assist, see workflow run.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@51c8f6ad4357d2ecc06e47120031b3d75e80227d

V8's Date constructor accepts strings like "ABC 6" by treating the
letter sequence as a timezone abbreviation. .NET DateTime.TryParse
rejects such strings as invalid.

Add a pre-validation check in parseRaw() that requires the input to
start with a digit or a recognised month-name prefix (Jan–Dec).
This matches .NET behaviour while preserving all existing valid formats.

Closes #3858

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@fable-repo-assist fable-repo-assist Bot added automation Automated changes bug javascript repo-assist Created by Repo Assist TypeScript labels May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Automated changes bug javascript repo-assist Created by Repo Assist TypeScript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Datetime.TryParse too forgiving in Javascript

0 participants