Is there any specific reason why DateDifference cannot give an exact number in this case?

DateDifference["Jan 2, 2013", "Jan 8, 2013", "Week"]


Also, I'd like to know how the various DayCountConvention settings interact with the calculation, in particular for "Week", "Month", etc.

  • $\begingroup$ That will return 1, yes. But why my example will not give 6/7? DateDifference will give a machine number in general in this case, when an exact number can be easily derived. $\endgroup$
    – asterix314
    Oct 29, 2013 at 7:09

1 Answer 1


The reason might be gleaned from examining the output from TracePrint.

DateDifference["Jan 2, 2013", "Jan 8, 2013", "Week"] // TracePrint (* Warning Huge Output *)

Somewhere close to the end of this humongous output we see the following:

DataPaclets`CalendarDataDump`n:Except[_Integer] :> N[DataPaclets`CalendarDataDump`n]
6/7 /. DataPaclets`CalendarDataDump`n:Except[_Integer] :> N[DataPaclets`CalendarDataDump`n]

So it appears that Mathematica computes the difference using AbsoluteTime which gives an exact number and afterwards (if the result is not an Integer) takes the numerical value. Hence the result you get. To get around this one can do:

Rationalize[DateDifference["Jan 2, 2013", "Jan 8, 2013", "Week"]]

Which gives:

{6/7, "Week"}

  • 1
    $\begingroup$ This is missing the point. I want to know why, Mathematica being what it is, does not give me 6/7 in the first place. $\endgroup$
    – asterix314
    Oct 29, 2013 at 7:24
  • $\begingroup$ +1 for tracing into the function and locating the N. But why use N when the result is not an integer? $\endgroup$
    – asterix314
    Oct 29, 2013 at 13:46
  • $\begingroup$ @asterix314. That I cannot explain. There's no clue as to why in that huge TracePrint output either. $\endgroup$
    – RunnyKine
    Oct 29, 2013 at 17:41
  • 3
    $\begingroup$ @asterix314 I think as users we cannot really answer the "why" question in a satisfactory way. RunnyKine's response shows why the behavior arises, but if you don't like it or want someone to justify it to you, I think you'll have to raise it with support or file it as a bug. $\endgroup$ Oct 29, 2013 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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