I have a list of strings,

{"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", "8434926"}

and I need to extract the date. It may be in any position.

Can I match a string to see if it is in "date" format, in this case ****-**-**, then extract it from the list?

  • 3
    $\begingroup$ Have you seen FromDateString[]? (And Check[]?) $\endgroup$
    – Michael E2
    Commented May 24, 2023 at 15:48
  • $\begingroup$ Does FromDateString give a different output if the input is not a date? I suppose this is what Check does. $\endgroup$
    – apg
    Commented May 24, 2023 at 15:50
  • $\begingroup$ So Check[FromDateString["2021-03-08"], 0], then remove the zeros. $\endgroup$
    – apg
    Commented May 24, 2023 at 15:54
  • $\begingroup$ I'm not sure exactly how you want to use it: Do you want to extract the strings that are dates, or do you want a Mathematica date object instead of a string. You could post-process the extracted strings, but that's an unnecessary step. (You could do what you suggest in your last comment, for instance. There may be better ways, though.) $\endgroup$
    – Michael E2
    Commented May 24, 2023 at 15:57
  • 1
    $\begingroup$ The format "ISODate" seems also to work. I'm not sure how flexible/rigid the formatting of the dates in your data is. $\endgroup$
    – Michael E2
    Commented May 24, 2023 at 16:08

4 Answers 4


The pattern "DatePattern[{"Year", "Month", "Day"}]" will match a date. You may use it e.g. inside "Select" like:

list = {"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", 
Select[list, StringMatchQ[#, DatePattern[{"Year", "Month", "Day"}]] &]

  • 1
    $\begingroup$ Holy cow I never even knew DatePattern was a thing, and it's been a thing since 2007! $\endgroup$
    – Pillsy
    Commented May 24, 2023 at 19:22

Extract the date strings:

dateStringQ[s_String] := 
  Quiet[Check[FromDateString[s, {"Year", "-", "Month", "-", "Day"}]; 
    True, False, FromDateString::str], FromDateString::str];

Cases[{"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", 
  "8434926"}, _?dateStringQ]

(*  {"2021-03-08"}  *)

Extract the date objects:

   {"BD4675365", "Corp", "Managers", "Syndicate", 
    "2021-03-08", "8434926"},
   {"Year", "-", "Month", "-", "Day"}],
  _?FailureQ] // Quiet

(*  {DateObject[{2021, 3, 8}, "Day"]}  *)
(Interpreter["Date"][#] & /@ list ) /. _Failure :> Nothing

Check[SemanticImportString[#, "Date"], Nothing] & /@ list // 
  Map[First] // Normal

enter image description here


It's always an overkill to use regex.

{"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", "8434926"} //

regex from https://stackoverflow.com/a/22061879/13040423


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.