Recursive AI

Extract dates and numbers from text in C#

OneNLP is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Extract dates and written numbers; resolve “tomorrow” against a reference date.

Terminal
dotnet add package RecursiveAI.OneNlp

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneNlp;
 
var reference = new DateTimeOffset(
    2026, 3, 2, 0, 0, 0, TimeSpan.Zero);
var dates = Nlp.Dates(
    "Ship on March 3, 2026 and follow up next Friday.",
    reference);
foreach (var date in dates)
    Console.WriteLine(
        $"{date.Text}: {date.Value}");
var numbers = Nlp.Numbers(
    "Order twenty books.");
foreach (var number in numbers)
    Console.WriteLine(
        $"{number.Text}: {number.Value}");
Text
March 3, 2026: 2026-03-03
next Friday: 2026-03-13
twenty: 20

Unzip the example, then dotnet run.

Full program

The input strings are “Ship on March 3, 2026 and follow up next Friday.” and “Order twenty books.” The fixed reference date is March 2, 2026 in UTC, so the result stays reproducible: the absolute date resolves to March 3, and “next Friday” resolves to March 13. Date recognition and number recognition use built-in rules; no model package is needed.

The download’s Program.cs is the snippet above.

dotnet run prints:

Text
March 3, 2026: 2026-03-03
next Friday: 2026-03-13
twenty: 20

Passing an unset default(DateTimeOffset) reference throws ArgumentException; text with no recognized dates or numbers returns no matches.

Variations

Extract a French date. Name the language when the text is not English. This standalone example uses the same reference date and the built-in French rules.

C#
using RecursiveAI.OneNlp;
 
var reference = new DateTimeOffset(2026, 3, 2, 0, 0, 0, TimeSpan.Zero);
foreach (var date in Nlp.Dates("le 4 mai 2026", reference, Language.French))
    Console.WriteLine($"{date.Text}: {date.Value}");
Text
le 4 mai 2026: 2026-05-04

The short answer

Use Nlp.Dates(text, reference) to extract dates and times from English text in C#. It recognizes expressions within a larger sentence rather than requiring the whole input to be a date string. Use Nlp.Numbers(text) for numbers, including written numbers such as “twenty”. Both calls work with the core RecursiveAI.OneNlp package and do not need a trained language model.

The reference is part of your input, not an incidental clock reading. In the example it is March 2, 2026: “March 3, 2026” is an absolute date, while “next Friday” resolves to March 13 against that reference. Store the reference with extracted results when you need to reproduce how a message was interpreted later. The recognizer documentation describes the returned values and supported expression types.

How do I extract a date from text in C#?

Call Nlp.Dates on the original string and enumerate the matches. Each match includes the exact matched text and UTF-16 start and end offsets. Keep those offsets with the original string if you want to highlight what was extracted. Rewriting or normalizing the string before highlighting would make those positions refer to different text.

The result type also carries the temporal kind. A date, time, duration and recurrence are not interchangeable. “Two weeks” is a duration, for example, not a calendar appointment with a known start. Inspect the kind and structured fields before putting a result into a database column that only accepts dates.

Relative phrases are resolved against the supplied reference and its offset. A fixed reference is useful in tests and replayed messages; the message’s actual timestamp may be more appropriate in production. An empty result means no rule recognized a match, not that the message cannot contain a date in some other notation.

Should I use DateTime.Parse to extract a date?

The .NET parsing APIs convert date strings into date values. They are not a search over all date-like phrases embedded in prose. Microsoft’s date parsing documentation distinguishes flexible parsing from exact-format parsing and explains culture-dependent interpretations.

Use exact parsing for a field whose format you control, such as an ISO date entered by a system. Use recognition for sentences containing expressions such as “tomorrow” alongside unrelated words. Those are different input contracts. Extracting a phrase and then applying a format parser does not by itself solve relative-date interpretation.

OneNLP returns a normalized Value together with richer fields such as the resolved range where available. Preserve those fields rather than round-tripping every result through a generic DateTime.Parse call. A range or recurrence cannot be faithfully reduced to one timestamp.

How do I handle different date formats and languages?

Choose the language explicitly with the overload shown in the variation. French and German interpret an ambiguous numeric date differently from the default English rules. For regional conventions, the lower-level recognizer accepts a RecognitionContext with a region, as documented in the recognizer guide.

The number path also returns normalized values. Quantity.Value is a decimal; units remain in their recognized units rather than being converted automatically. Keep the unit and kind with the value when extending this example to measures, money or percentages. Otherwise, two identical numeric values may mean very different things.

All onenlp recipes