Extract Date From Text in Excel Using TEXTBEFORE and TEXTAFTER

📅 Jan 20, 2026 📝 Sarah Miller

Extracting messy dates from unstructured text strings in Excel is a persistent pain point for data analysts seeking clean reports. While standard funding sources for enterprise-level IT overhauls or complex legacy formulas offer traditional workarounds, they often lack agility. Fortunately, mastering the combination of TEXTAFTER and TEXTBEFORE grants users immediate, dynamic control over their data parsing without expensive add-ins. Note the stipulation that these functions require Microsoft 365 or Excel for the Web. For example, isolating "2023-10-25" from "ID_2023-10-25_Final" is now seamless. Below, we break down the precise formula syntax to elevate your spreadsheet workflow.

Extract Date From Text in Excel Using TEXTBEFORE and TEXTAFTER

Data entry in the real world is rarely perfect. Often, you will find yourself dealing with imported databases, reports, or logs where critical pieces of information-like dates-are trapped inside long strings of text. For years, Excel users had to rely on complex, hard-to-read formulas combining MID, LEFT, RIGHT, FIND, and SEARCH to isolate these dates.

Fortunately, Microsoft introduced a suite of powerful text manipulation functions in Excel 365 and Excel 2021. Among these, TEXTAFTER and TEXTBEFORE stand out as game-changers. By combining these two functions, you can easily slice through surrounding text, isolate a date, and convert it into a fully functional Excel date serial number. In this comprehensive guide, we will explore exactly how to use these formulas to extract dates from text under various real-world scenarios.


Understanding the Anatomy of TEXTBEFORE and TEXTAFTER

Before diving into date extraction formulas, let's briefly look at how these two functions operate. Their syntax is straightforward, yet highly customizable.

The TEXTAFTER Function

This function returns all text that occurs after a specific character or substring (the delimiter).

=TEXTAFTER(text, delimiter, [instance_num], [match_mode], [match_end], [if_not_found])
  • text: The text string or cell reference you want to search.
  • delimiter: The character or word that marks where you want to start extracting.
  • instance_num: (Optional) If the delimiter appears multiple times, specifies which one to use.

The TEXTBEFORE Function

This function does the exact opposite; it returns all text that occurs before a designated delimiter.

=TEXTBEFORE(text, delimiter, [instance_num], [match_mode], [match_end], [if_not_found])

By nesting these functions-using TEXTAFTER to strip away everything to the left of the date, and then TEXTBEFORE to strip away everything to the right-we can cleanly isolate any date string.


Scenario 1: Extracting a Date Located Between Known Delimiters

Consider a common scenario where you receive a system log or order export that looks like this in cell A2:

Order processed on 2023-11-15 by warehouse.

Our goal is to extract "2023-11-15". We can see that the date is consistently positioned after the word "on " and before the word " by".

Step-by-Step Formula Build

Step 1: Use TEXTAFTER to isolate everything after the word "on ":

=TEXTAFTER(A2, "on ")

Result: "2023-11-15 by warehouse."

Step 2: Nest that result inside a TEXTBEFORE function to grab everything before " by":

=TEXTBEFORE(TEXTAFTER(A2, "on "), " by")

Result: "2023-11-15"

While this looks like a date, Excel still treats it as a text string. To perform sorting, calculations, or timeline analysis, we must convert it to a true Excel date value.

Converting Text to an Actual Date Value

To convert the extracted string into a native date serial number, simply apply a double unary operator (--) or wrap the entire formula in the DATEVALUE function:

=--TEXTBEFORE(TEXTAFTER(A2, "on "), " by")

Once you press Enter, apply a Date format (such as Short Date) to the destination cell via the Home tab in your ribbon, and Excel will treat it as a proper date.


Scenario 2: Extracting Dates from Standard File Names

Data analysts often process lists of files generated by automated systems, such as:

  • Report_08-23-2024_Final.xlsx
  • Report_12-05-2023_Draft.xlsx

In this case, the date is surrounded by underscores (_). We can target the underscores as our delimiters.

The Formula:

If the file name is in cell A2, we want to extract the text between the first and second underscores.

=--TEXTBEFORE(TEXTAFTER(A2, "_"), "_")

How this works:

  1. TEXTAFTER(A2, "_") searches for the first underscore and returns everything to its right: "08-23-2024_Final.xlsx".
  2. TEXTBEFORE(..., "_") takes that result and extracts everything before the next underscore: "08-23-2024".
  3. The double negative (--) forces Excel to interpret the text "08-23-2024" as a serial date number (which is 45526), ready to be formatted as a date.

Scenario 3: Handling Dates When Delimiters Vary (Using Instance Numbers)

Sometimes your text strings are highly irregular, but the position of the delimiters remains structured. Imagine a text string containing multiple hyphens:

TX-DAL-05/12/2024-PR-992

Here, the date 05/12/2024 is nestled between the second hyphen and the third hyphen. We can leverage the instance_num parameter of both functions to pinpoint this exact location.

The Formula:

=--TEXTBEFORE(TEXTAFTER(A2, "-", 2), "-")

The Breakdown:

  • TEXTAFTER(A2, "-", 2) tells Excel to look for the second hyphen in the string and return everything after it. This leaves us with: "05/12/2024-PR-992".
  • We then wrap this in TEXTBEFORE, targeting the first hyphen of this new, shorter string. This isolates "05/12/2024".
  • The double negative converts it to a standard Excel date serial number.

Scenario 4: Handling Errors Gracefully

When working with large datasets, some cells might not contain the text pattern your formulas expect. If your formula fails to find a delimiter, Excel will throw a #N/A error. To prevent this from ruining your dashboard or reports, you should build error-handling directly into your calculation flow.

Option A: Using the Built-In if_not_found Argument

Both TEXTBEFORE and TEXTAFTER include an optional final parameter called if_not_found. This allows you to define a fallback result directly within the function syntax itself:

=TEXTBEFORE(TEXTAFTER(A2, "on ", , , , "No Date"), " by", , , , "No Date")

Option B: Wrapping in IFERROR

Because converting a text value to a date using -- can also trigger a #VALUE! error if the extracted text isn't actually a date, the safest practice is to wrap your complete formula in an IFERROR statement:

=IFERROR(--TEXTBEFORE(TEXTAFTER(A2, "on "), " by"), "Invalid or Missing Date")

This approach catches missing delimiters as well as conversion failures (e.g., if the text between "on " and " by" was accidentally written as "yesterday" instead of an actual date).


Pro-Tip: When Regional Date Settings Cause Issues

If you extract a date text string structured as "15/12/2023" (DD/MM/YYYY) but your computer is set to US regional settings (MM/DD/YYYY), the double negative (--) or DATEVALUE functions will return a #VALUE! error because Excel cannot recognize "15" as a valid month.

If you encounter this regional formatting barrier, you can extract the day, month, and year separately using nested text formulas, then reconstruct them cleanly using Excel's native DATE function:

=DATE(year, month, day)

By extracting segments with precision using TEXTBEFORE and TEXTAFTER, you can bypass regional issues entirely, ensuring your spreadsheets operate flawlessly no matter who is viewing them globally.


Summary of Benefits

Feature Old Methods (MID, FIND, SEARCH) Modern Method (TEXTBEFORE / TEXTAFTER)
Formula Length Long, deeply nested, and hard to debug. Short, intuitive, and highly readable.
Robustness Easily breaks if text lengths shift dynamically. Target-driven based on specific, logical markers.
Instance Control Requires manual math to find the Nth character. Built-in instance parameter (e.g., grab the 2nd delimiter).

By mastering these text-slicing functions, you can automate what used to be a highly tedious data cleaning process, saving time and keeping your Excel worksheets dynamic, clean, and error-free.

Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.