between - Calendar math differences - MATLAB (original) (raw)

Calendar math differences

Syntax

Description

The between function returns the calendar differences between two datetime arrays. When between calculates the calendar difference between two datetime values, it takes into account leap years, Daylight Saving Time shifts, and the differing lengths of calendar months. To calculate differences as exact fixed-length durations, use subtraction instead, as in dt = t2 - t1.

[dt](#bugdaj2-1-dt) = between([t1](#bugdaj2-1-t1),[t2](#bugdaj2-1-t2)) returns the differences between the datetime values in t1 andt2. The dt output is acalendarDuration array in terms of the calendar components years, months, days, and time, such that t2 = t1+dt.

example

[dt](#bugdaj2-1-dt) = between([t1](#bugdaj2-1-t1),[t2](#bugdaj2-1-t2),[components](#bugdaj2-1-components)) returns the differences between datetime values in terms of the specified calendar or time components.

example

Examples

collapse all

Create two datetime arrays.

t1 = datetime 01-Feb-2025 09:02:12

t2 = datetime('tomorrow','Format','dd-MMM-yyyy HH:mm:ss') + caldays(0:2)

t2 = 1×3 datetime 02-Feb-2025 00:00:00 03-Feb-2025 00:00:00 04-Feb-2025 00:00:00

Find the difference between the two arrays.

dt = 1×3 calendarDuration 14h 57m 47.649s 1d 14h 57m 47.649s 2d 14h 57m 47.649s

between returns a calendarDuration array containing differences in terms of days, hours, minutes, and seconds.

Create a sequence of datetimes over a 6-month period. Then, find the number of days between the first date and each of the dates in the sequence.

t1 = datetime(2013,1,1); t2 = dateshift(t1,'end','month',0:4)

t2 = 1×5 datetime 31-Jan-2013 28-Feb-2013 31-Mar-2013 30-Apr-2013 31-May-2013

dt = between(t1,t2,'Days')

dt = 1×5 calendarDuration 30d 58d 89d 119d 150d

Input Arguments

collapse all

Input date and time, specified as a datetime array, character vector, cell array of character vectors, or string array. At least one of inputs t1 and t2 must be adatetime array. t1 andt2 must be the same size unless one is a scalar.

Input date and time, specified as a datetime array, character vector, cell array of character vectors, or string array. At least one of inputs t1 and t2 must be adatetime array. t1 andt2 must be the same size unless one is a scalar.

Calendar or time components, specified as one of the following character vectors, or a cell array or string array containing one or more of these values:

Except for 'time', the above components are flexible lengths of time. For example, one month represents a different length of time when added to a datetime in January than when added to a datetime in February.

between operates on the calendar or time components in decreasing order, starting with the largest component.

In general, t2 is not equal to t1 + dt, unless you include 'time' incomponents.

Example: {'years','quarters'}

Data Types: char | cell | string

Output Arguments

collapse all

Difference array, returned as a calendarDuration array.

Tips

Extended Capabilities

expand all

Thebetween function fully supports tall arrays. For more information, see Tall Arrays.

Version History

Introduced in R2014b