Working with dates in Excel can be challenging, especially when they are embedded in strings. Whether you're a beginner or an experienced Excel user, mastering the art of extracting and changing date formats is crucial for efficient data management. This article delves into the practical steps of how to extract dates from strings and how to change date formats in Excel, ensuring your spreadsheets are both accurate and user-friendly.
Note that, there isn't a single formula or approach that can handle all scenarios. Extracting dates from strings that is not following a specific pattern is one of the few things Excel cannot handle. You almost need an AI to be able to encapsulate every possible scenario. The good thing is that we are going to offer you multiple ways to parse the date.
Understanding Date Formats in Excel
Excel stores dates as serial numbers, a system that may initially seem abstract but is highly efficient. This serial number format represents dates as a count of days from a specific starting point: January 1, 1900, is designated as serial number 1. As a result, each subsequent day increments this number by one. For example, January 2, 1900, is represented as 2, and so forth. This numerical representation simplifies various date-related operations, such as calculating the number of days between two dates, or adding a certain number of days to a given date. The system extends to time as well, where time is represented as a fractional part of a day. For instance, 12:00 PM, which is halfway through a day, is represented as 0.5. This integration of dates and times into a unified numerical system is what makes Excel a powerful tool for date and time calculations.
However, this efficient system encounters a limitation when dates are embedded within strings. In cases where dates are part of a text string, Excel does not recognize these as date values due to its inherent design to perceive text and numbers differently. Consequently, the date within the string is treated as plain text, making it incompatible with Excel's date-specific functions and calculations. This limitation necessitates the extraction of the date from the string to leverage Excel's date functionalities.
We have generated strings that included dates using slash (/) as day-month-year separator and using US-type (month/day/year) dates. Thus, if your date format is different, you may see errors (empty cells because of IFERROR functions) after opening our file. Update the dates according to your localization settings. Also, do not forget to update the slash characters if your data includes dates with other separators like dot (.) or hyphen (-).
Formula for extracting date from string
The foundational principle behind the formulas used for extracting dates from strings in Excel hinges on pattern recognition. Essentially, the task involves scanning the string to identify a sequence of characters that matches the common structure of a date. This pattern could vary – from standard formats like 'MM/DD/YYYY' to more complex or regional variants like 'DD-MM-YYYY' or 'YYYY.MM.DD'. Once the formula identifies this pattern, indicating the presence of a potential date within the string, the next step is parsing.
The tricky part is to select a correct pattern which is suitable with your raw data. A date can be in many forms:
- 01/07/2022
- 1/7/2022
- 1/7/22
- 1/12/2022
- 12/1/2022
- 12/1
- 12/2022
If your raw data contains more than one or two patterns the formula solution may not work unless you do not want to create extremely long formulas.
In our sample formula, we used "01/07/2022" format. Thus, we assume the date values in the data to follow the rules below:
- US Date format
- Month and day are two digits
- Year uses four digits
- Each unit is separated by slash "/"
- A date contains 10 characters including delimiters
Based on these assumptions, we can use "/??/" keyword with the SEARCH function to locate the date. The SEARCH function can use the wildcards to locate a pattern instead of an exact string. Because a question mark (?) represents a single character, "/??/" refers 2 characters between slashes just like the day part of a date.
The SEARCH function returns the position number where the keyword starts if there is a matching string.
Subtract the position by 2 to find where the date starts.
Once the date is located, you can use the MID function to parse it. The MID function needs the position of the string you want to parse and its length. Because we assume the date format is "mm/dd/yyyy", the length will be 10.
Although, the formula looks like its working, it will return any characters matching the pattern i.e. letters.
This is where the DATEVALUE function comes handy. The function can convert text-based date/time values into actual dates. The function returns #VALUE! error if the supplied text is not a valid date. You can use the DATEVALUE function to verify dates. The IFERROR function can wrap the formula to cover scenarios without date.
Although this formula is suitable for our example, it's obvious that it will fail for others. A few examples:
- A single-digit month date without a space in front: cells C6, C9
- A text containing similar string before the actual date: cell C8
- Single-digit month and days: cells between C19 and C25
- Double-digit years: cells between C26 and C32
Using VBA for extracting date from string
For those seeking more customized solutions in Excel, particularly in the context of changing date formats, an effective alternative to standard Excel formulas is the creation of User Defined Functions (UDFs) using Visual Basic for Applications (VBA). This approach involves writing VBA scripts that are tailored to specific requirements, such as intricately changing or extracting date formats within Excel. These UDFs, once written and properly saved within the Excel VBA editor, can be utilized just like any built-in Excel function. This means you can easily call these functions in your spreadsheet to perform specialized tasks, such as converting dates from one format to another or extracting dates from complex strings. Integrating UDFs into your Excel toolkit significantly expands your capabilities in managing date formats, offering a level of customization and flexibility that goes beyond the constraints of pre-existing Excel functions. This method is particularly valuable for those wondering how to change date format in Excel in more advanced or specific ways, as it allows for the creation of bespoke solutions that can be tailored to unique data processing scenarios, enhancing both the efficiency and effectiveness of your Excel workflows.
VBA has two dedicated methods which give a particular advantage for extracting a date from a string.
- The Split method can separate words or strings separated by spaces.
- The DateValue method can validate dates better than its formula counterpart.
Obviously, this method is not foolproof neither. The date values should be separated from other text by a space character. On the other hand, you are not limited with a certain pattern. Thus, number of digits in units are not significant.
You can copy and paste the following code into a module and start using it right away. Just do not forget to save your Excel file as an Excel Macro-Enabled Workbook (XLSM) rather than a regular workbook.
Function ExtractDate(str As String)
On Error GoTo errHandler
Dim i As Integer, dt As Date, arr() As String, temp As String
dt = 0
arr = Split(str)
For i = LBound(arr) To UBound(arr)
temp = arr(i)
If IsDate(temp) And Len(temp) > 5 Then
dt = DateValue(temp)
Exit For
End If
Next i
cont:
ExtractDate = dt
Exit Function
errHandler:
dt = 0
Resume cont
End Function
Once you paste the code, use it in your worksheet by giving a reference to the data cell.
The equals to zero (0) condition is to avoid zero (0) values. Otherwise, the function will return zero (0) when it cannot find a valid date.
Extracting and changing date formats in Excel might seem daunting at first, but with these straightforward steps, you can efficiently manage and transform your date data. Whether it's for project timelines, financial records, or any other type of data analysis, mastering these skills will greatly enhance your Excel proficiency.








