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.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    8 June 2009
    2009-06-08
  3. On the Match panel, set “begin regex match at” to “start of word”, and set “end regex match at” to “end of word”.
  4. Click the Add First Field button to add field 1.
  5. In the “pattern to match field” drop-down list, select “date and time”.



  6. 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
  7. Select “optional leading zeros” in the “leading zeros” drop-down list.
  8. 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
  9. Set the “field validation mode” to “average”.
  10. On the Regex panel, select “C# (.NET 2.0–4.8 & .NET Core 1.0–3.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
    # 1. Date and time
    (?:
    # d M Y
    (?: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}|
    # Y/m/d
    [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.

  11. The Samples panel now highlights the dates our regex matches:
    8 June 2009
    2009-06-08

Related Examples

Reference