Returns the difference between two supplied dates as date components.
- iOS 2.0+
- macOS 10.4+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Specifies the components for the returned
The start date for the calculation.
The end date for the calculation.
Options for the calculation. For possible values, see
If you specify a “wrap” option (
wrap), the specified components are incremented and wrap around to zero/one on overflow, but do not cause higher units to be incremented. When the wrap option is not specified, overflow in a unit carries into the higher units, as in typical addition.
NSDate object whose components are specified by
unit and calculated from the difference between the
start using the options specified by
nil if either date falls outside the defined range of the receiver or if the computation cannot be performed.
The result is lossy if there is not a small enough unit requested to hold the full precision of the difference. Some operations can be ambiguous, and the behavior of the computation is calendar-specific, but generally larger components will be computed before smaller components; for example, in the Gregorian calendar a result might be 1 month and 5 days instead of, for example, 0 months and 35 days. The resulting component values may be negative if
result is before
The following example shows how to get the approximate number of months and days between two dates using an existing calendar (
Note that some computations can take a relatively long time.