# same as DateDifference, but faster?

Posted 11 years ago
2570 Views
|
2 Replies
|
4 Total Likes
|
 Given that: In[201]:= DateDifference[{2011, 1, 19}, {2011, 1, 18}]Out[201]= -1any alternate/creative methods to do the same thing faster? In[195]:= Timing[Do[DateDifference[{2011, 1, 19}, {2011, 1, 18}], {1000}];]Out[195]= {1.934, Null}
2 Replies
Sort By:
Posted 11 years ago
 If you're only interested in number of days and the dates in question don't go through any historical calendar changes, you could use AbsoluteTime to convert to seconds between and then convert seconds to days: In[11]:= DateDifference[{2011, 1, 19}, {2011, 1, 18}]Out[11]= -1In[12]:= Timing[Do[DateDifference[{2011, 1, 19}, {2011, 1, 18}], {1000}];]Out[12]= {2.074813, Null}In[13]:= newDateDiff[d1_, d2_]:= Round[(AbsoluteTime[d2] - AbsoluteTime[d1])/(24*60*60)]In[14]:= newDateDiff[{2011, 1, 19}, {2011, 1, 18}]Out[14]= -1In[15]:= Timing[Do[newDateDiff[{2011, 1, 19}, {2011, 1, 18}], {1000}];]Out[15]= {0.015600, Null}
Posted 11 years ago
 There was a detailed discussion about this on StackExchange.  You'll find fast implementations in the answers there.There were also some reports that this function got slower in 9 compared to 8.I haven't personally tested any of this.