4
$\begingroup$

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)

$\endgroup$
  • $\begingroup$ Use DatePattern $\endgroup$ – Mike Honeychurch Jun 4 '14 at 23:11
5
$\begingroup$

A simpler version very similar in spirit to your own solution:

dateQ = Composition[Quiet, NumberQ, AbsoluteTime]
$\endgroup$
  • $\begingroup$ Very elegant. Accepted $\endgroup$ – PlaysDice Jun 4 '14 at 20:00
2
$\begingroup$

Rather than creating your own function, the built in way to check any string is to use StringMatchQ and DatePattern. From the docs:

ref/DatePattern

enter image description here

$\endgroup$
  • $\begingroup$ Looks like this is the right way! $\endgroup$ – Szabolcs Jun 4 '14 at 23:44
  • $\begingroup$ Mike- nice and a +1. I'm going to stick with Szabolcs' approach as it works across all the test cases without prior knowledge of the formatting. The use-case was checking data from a spreadsheet that had a variety of date formats. $\endgroup$ – PlaysDice Jun 5 '14 at 0:10
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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