Pattern: Date and time
“Date and time” is one of the patterns that you can select on the Match panel. Use this pattern to make a field match a date, a time, or any part of or any combination of date and time indicators.
This example shows how you can use the “date and time” pattern to look for a date in various formats. You can find this example as “Pattern: date and time” in the RegexMagic library.
- Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
- On the Samples panel, paste in one new sample:
8 June 2009
2009-06-08
- On the Match panel, set “begin regex match at” to “start of word”, and set “end regex match at” to “end of word”.
- Click the button to add field .
- In the “pattern to match field” drop-down list, select “date and time”.
- Enter the names of the months using semicolons to delimit different months, and commas to delimit different spellings of the same month:
Jan,January;Feb,February;Mar,March;Apr,April;May,May;Jun,June;Jul,July;Aug,August;Sep,September;Oct,October;Nov,November;Dec,December
- Select “optional leading zeros” in the “leading zeros” drop-down list.
- Enter the date formats we want to allow, one per line. Right-click the “allowed date and time formats” box to select the format specifiers from a menu.
d M Y
Y/m/d
- Set the “field validation mode” to “average”.
- On the Regex panel, select “C# (.NET 2.0–8.0)” as your application, turn on free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
\b
(?:
(?:3[01]|[12][0-9]|0?[1-9])[\t ]+(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)[\t ]+[0-9]{4}|
[0-9]{4}[/.-](?:1[0-2]|0?[1-9])[/.-](?:3[01]|[12][0-9]|0?[1-9])
)
\b
Required options: Case insensitive; Free-spacing.
Unused options: Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture.
- The Samples panel now highlights the dates our regex matches:
8 June 2009
2009-06-08
Related Examples
Reference