This answer notes that Calendar
package has a function DateQ
but the documentation suggests that the input is limited to a fixed format YY,MM,DD
etc. Is there a more robust way to check any String for date-form ?
(Self answer, below)
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityThis answer notes that Calendar
package has a function DateQ
but the documentation suggests that the input is limited to a fixed format YY,MM,DD
etc. Is there a more robust way to check any String for date-form ?
(Self answer, below)
A simpler version very similar in spirit to your own solution:
dateQ = Composition[Quiet, NumberQ, AbsoluteTime]
Rather than creating your own function, the built in way to check any string is to use StringMatchQ
and DatePattern
. From the docs:
One basic approach:
refDate = "Jan 1 2012";
dateQ[input_] :=
!TrueQ@Quiet@Check[DateDifference[input, refDate], True]
Testing
test={"dog",dog,"Jan 1 2012", "January 1 2012", "1/1/2012","1/1/12","Q2 2014", "2014 Q1","2014Q2"};
dateQ/@test
(*OUTPUT*)
{False, False, True, True, True, True, True, True, True}
Of course, this approach could be further modified to pass values for Calander->
and suitable refDate
to the function.
DatePattern
$\endgroup$