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.

If you run this code you will see that DatePlus and DateList are inconsistent in the format of the lowest order element of the date list.

DatePlus[DateList["2012-1-1" ], {5, "Month"}] === DateList["2012-6-1"]
DatePlus[DateList["2012-1-1" ], {5, "Month"}] == DateList["2012-6-1"]
DateList[DatePlus[DateList["2012-1-1" ], {5, "Month"}]] === DateList["2012-6-1"]
DatePlus[DateList["2012-1-1" ], {5, "Month"}] // FullForm
DateList["2012-6-1"] // FullForm
DateList[DatePlus[DateList["2012-1-1" ], {5, "Month"}] ] // FullForm

This leads to issues when you try to locate elements in a list as follows:

Position[{DatePlus[DateList["2012-1-1" ], {5, "Month"}]}, DateList["2012-6-1"]]
Position[{DateList[DatePlus[DateList["2012-1-1" ], {5, "Month"}]]}, DateList["2012-6-1"]]

What is even more annoying is that I have found that wrapping a date generated by DatePlus in DateList doesn't always fix the problem and I can not tell what circumstances are required to make it work and not work.

Any insight would be appreciated. I may try to open a case with Wolfram, but my past experiences with their support team have been an unsatisfactory use of my time.

share

2 Answers 2

up vote 2 down vote accepted

As you note yourself Equal handles the problem, and in fact that is typically how these cases are handled:

Position[
  {DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
  x_ /; x == DateList["2012-6-1"]
]

Position[
  {DateList[DatePlus[DateList["2012-1-1"], {5, "Month"}]]}, 
  x_ /; x == DateList["2012-6-1"]
]
{{1}}

{{1}}

If you prefer PatternTest:

Position[
  {DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
  _?(# == DateList["2012-6-1"] &)
]

Position[
  {DateList[DatePlus[DateList["2012-1-1"], {5, "Month"}]]}, 
  _?(# == DateList["2012-6-1"] &)
]
{{1}}

{{1}}
share
    
Not sure of the etiquette here, but thanks for the responses, that's just what I needed. –  vandel Jul 18 '12 at 12:05
    
@vandel you're welcome, and thanks. The etiquette is to Accept (check mark) an answer that fully satisfies the question, just as you did. –  Mr.Wizard Jul 18 '12 at 20:19

These issues revolving around casting to integer vs. float abound whenever translating data to/from external formats like Excel.

In all cases, projecting away the time information from the date proper with Take[#, 3]& or Drop[#, -3] solves your issues, but it is a hack nevertheless.

Take[#, 3] &@DatePlus[DateList["2012-1-1"], {5, "Month"}] === Take[#, 3] &@DateList["2012-6-1"]

yields True and

Position[{Take[#, 3] &@DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
 Take[#, 3] &@DateList["2012-6-1"]]

yields {{1}}

Of course, Round also works:

Position[{DatePlus[DateList["2012-1-1"], {5, "Month"}]}, 
 Round /@ DateList["2012-6-1"]]
(* {{1}} *)

If you do contact WRI again, I found that they're helpful but resource-constrained and typically promise to put your suggestion on their feature implementation queue. Perhaps referencing this link would help, as these issues are nagging in data analysis.

share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .