# Matching DateObject with Cases

OK, here is something that is me missing the obvious.

I have an Excel spreadsheet consisting of 2 columns and 5 rows. The first column is formatted in Excel as a dd-mmm-yy date. The second column is a string.

I import the file into Mathematica:

testData = Flatten[Import["testData.xlsx","Data"],1]


I want to use Cases to select the rows that belong to September 3:

myDate = DateObject[{2017,9,3,0,0,0}]


Cases[testData,{myDate,_}]


but this gives as output:

{}


What am I missing? Thanks much in advance.

• The proper DateObject probably has a lot of meta-info tagged onto it. Extract one of them and look at its FullForm and use the pattern you derive from that for you myDate. My guess is you'll want to do something like DateObject[{2017, 9, 3, ___}, ___] Oct 26, 2017 at 19:30
• Thank you for your help. There is indeed metadata: DateObject[{2017,9,3,0,0,0],"Instant","Gregorian",-7.] Oct 26, 2017 at 20:03
• I mean myDate probably captured some of that. I dunno if it would have captured all, though. Did this pattern work: DateObject[{2017, 9, 3, ___}, ___]? Oct 26, 2017 at 20:05
• Yes I got it to work. Oct 26, 2017 at 20:06
• Great. Post what you did as an answer so people know this has been dealt with. Oct 26, 2017 at 20:06

You may use DateWithinQ with a DateObject of "Day" calendar granularity within Select.

With

testData = Transpose[{DateObject@DateRange[{2017, 08, 31}, {2017, 9, 4}, "Day"], Range[5]}]


and

targetDate = DateObject[{2017, 9, 3}, "Day"]


Notice that targetDate is of "Day" granularity.

Then

Select[DateWithinQ[targetDate, First@#] &]@testData


Since targetDate is of "Day" granularity any DateObject within the day (i.e. all dates of the day at any time within the day) with match in DateWithinQ.

Hope this helps.

• The situation is reminiscent of picking out rows that contain inexact numbers with a predicate that depends on equality, where using Cases[] is clunky, and Select[] is the better choice. Oct 27, 2017 at 2:36

As b3m2a1 points out, there is a lot of metadata tacked on to DateObjects, see my comment above.

Cases[testData,{DateObject[{2017,9,3,___},___],_}]


works.

• There's a simpler solution. Use myDate = DateObject[{2017, 9, 3, 0, 0, 0.}] note the seconds value has a decimal point. With this change, your original Cases` works for me. Oct 26, 2017 at 20:15
• Yes this works too. Thank you Oct 26, 2017 at 21:02