Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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)

share|improve this question
    
Use DatePattern –  Mike Honeychurch Jun 4 at 23:11

3 Answers 3

up vote 4 down vote accepted

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

dateQ = Composition[Quiet, NumberQ, AbsoluteTime]
share|improve this answer
    
Very elegant. Accepted –  PlaysDice Jun 4 at 20:00

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

share|improve this answer
    
Looks like this is the right way! –  Szabolcs Jun 4 at 23:44
    
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. –  PlaysDice Jun 5 at 0:10

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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