DateDifference
DateDifference[date1,date2]
gives the number of days from date1 to date2.
DateDifference[date1,date2,"unit"]
gives the difference between date1 and date2 in the specified unit.
DateDifference[date1,date2,{"unit1","unit2",…}]
gives the difference as a list with elements corresponding to the successive "uniti".
Details and Options
- DateDifference computes the amount of time between two given dates, expressing the result either as a physical duration or as a number of calendar steps.
- DateDifference returns a Quantity object.
- Dates can be specified in the following forms:
-
DateObject[…] date object {y,m,d} year, month, day {y,m} the first day of the specified month {y} January 1 of the year y "string" date as a string ("Jan. 1, 2022") {y,m,d,h,m,s} precise time time absolute time specification - Possible units are "Year", "Quarter", "Month", "Week", "Day", "Hour", "Minute", "Second".
- {y,m,d} is taken to be equivalent to {y,m,d,0,0,0} etc.
- DateDifference takes the following option:
-
DayCountConvention "ActualActual" counting convention by which the difference is computed Method Automatic date arithmetic method to use - For added time steps with variable duration (e.g. "Month"), date arithmetic can either convert the steps into a physical duration (continuous arithmetic) or treat them all as discrete calendar steps (discrete arithmetic).
- The option value Method"Continuous" implements continuous arithmetic. Discrete arithmetic is implemented with Methodrolling, where possible rolling methods are "RollForward", "RollBackward" and "RollOver", with the default being "RollBackward".
- For discrete arithmetic, adding, for example, a month may result in a date that does not exist (e.g. February 30). In such cases, there are three possible options for how that date should be resolved:
-
"RollBackward" roll back to the last existing date (Feb 28) "RollForward" roll forward to the next existing date (Mar 1) "RollOver" increment from the next existing date (Mar …) - DateDifference enumerated in days follows the day count convention, regardless of the magnitude of the difference.
- DateDifference[date2] gives the fractional number of days from the current time until date2.
Examples
open allclose allBasic Examples (4)
Scope (6)
Date Formats (3)
Give dates in any format supported by DateString:
Different date specifications can be mixed:
DateDifference can use times given in {y,m,d,h,m,s} format:
Options (6)
DayCountConvention (2)
CalendarType (2)
Method (2)
By default, May 31 is three months prior to June 30:
Using Method"RollForward", there are two months and 30 days separating the dates:
Compare the results of different arithmetic methods when comparing two dates:
Applications (2)
Properties & Relations (1)
DatePlus adds time units to dates, while DateDifference computes units between dates:
Possible Issues (2)
Date strings can be ambiguous:
Use DateObject to disambiguate between different interpretations:
In previous Wolfram Language releases, DateDifference returned purely numerical results. This legacy behavior can be enabled with SetSystemOptions:
Quantity results can be re-enabled by setting the same system option value to True:
Text
Wolfram Research (2007), DateDifference, Wolfram Language function, https://reference.wolfram.com/language/ref/DateDifference.html (updated 2022).
CMS
Wolfram Language. 2007. "DateDifference." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/DateDifference.html.
APA
Wolfram Language. (2007). DateDifference. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DateDifference.html