What exactly is the difference between Interval.length()
, Interval.count()
, and DateTime.diff()
?
#1434
-
I'm having some trouble understanding the nuanced differences between these three similar-looking methods. I ran some experiments in a Codesandbox, but it's still not super clear: https://codesandbox.io/s/luxon-date-range-test-3uog3b?file=/src/App.tsx Assuming a range of DateTime.diff()"Return the difference between two DateTimes as a Duration." OK, that seems straightforward enough. But then:
Interval.length()"Returns the length of the Interval in the specified unit."
Interval.count()"Returns the count of minutes, hours, days, months, or years included in the Interval, even in part. Unlike Interval#length this counts sections of the calendar, not periods of time, e.g. specifying 'day' asks 'what dates are included in this interval?', not 'how many days long is this interval?'"
I don't understand this explanation at all. What does "What dates are included in this interval" mean, compared to "how many days long is this interval"? Is that "is included" a binary true/false count of days in that (so it'll give you an int)? Is that the same as "will round up to the nearest integer of the specified unit", or is there some other nuance I'm not getting? In summaryWould it be correct say this:
Or am I missing something...? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
DurationsDurations are a pile of units. Think of it as like Interval.length(unit) vs Duration.as(unit)These are not always the same. See here Interval.countNo, |
Beta Was this translation helpful? Give feedback.
Durations
Durations are a pile of units. Think of it as like
{ days: 1, hours: 48, minutes: 3, seconds: 4 }
. The various methods likeshiftTo
can move data between the units. Anyway, that duration would return1
for days, since the day component is1
.as("days")
converts it to days and would return 3.something. If you don't specify the units in thediff()
call, you get the diff in milliseconds (i.e. the duration is like{milliseconds: someHugeNumber}
, so thedays
component is 0. See here for more.Interval.length(unit) vs Duration.as(unit)
These are not always the same. See here
Interval.count
No,
count("days")
checks how many calendar days are included in the interval. So if the interval…