Main Content

calquarters

R2026b

Calendar duration in quarters

Description

The calquarters function returns an array that represents calendar quarters. When you use arrays that have the calendarDuration data type in calendar calculations, the calculations take into account leap years, Daylight Saving Time shifts, and the differing lengths of calendar months. For calculations that involve fixed lengths of time, use the duration data type.

Q = calquarters(X) returns an array representing calendar quarters equivalent to the values in array X.

  • If X is a numeric array, then Q is a calendarDuration array with each element equal to the number of calendar quarters in the corresponding element of X.

  • If X is a calendarDuration array, then Q is a double array with each element equal to the number of whole calendar quarters in the corresponding element of X.

    Q = fix(calmonths(t)/3)

example

Q = calquarters(X,ArithmeticMethod=arithmeticMethod) specifies how Q handles arithmetic with datetime values. For this syntax, X must be a numeric array.

example

Examples

collapse all

X = magic(4);
Q = calquarters(X)
Q = 4×4 calendarDuration
   16q    2q    3q   13q
    5q   11q   10q    8q
    9q    7q    6q   12q
    4q   14q   15q    1q

Create a datetime value whose date component is the end of a quarter.

D = datetime(2021,9,30)
D = 

   datetime

   30-Sep-2021

Create a calendar quarter with end-of-quarter arithmetic by using the calquarters function. Then, add it to D.

C = calquarters(1,ArithmeticMethod="endofquarter")
C =

   calendarDuration (end of quarter arithmetic)

   1q
D2 = D + C
D2 =

   datetime

   31-Dec-2021

Because D occurs at the end of a quarter, using end-of-quarter arithmetic preserves the quarter-end date and results in the date of December 31st.

Create an array of calendar durations. Then, convert each value to the equivalent number of whole calendar quarters.

X = calmonths(2:2:6) + caldays(8)
X = 1×3 calendarDuration
   2mo 8d   4mo 8d   6mo 8d

Q = calquarters(X)
Q = 1×3

     0     1     2

Input Arguments

collapse all

Input array, specified as a numeric array, calendar duration array, or logical array. If X is a numeric array, it must contain only integer values. That is, you cannot create fractional calendar units.

Method for handling arithmetic with datetime values, specified as one of the values in this table.

ValueDescription

"default"

In datetime arithmetic, preserve the day of the month when the resulting month contains that day. Otherwise, use the last day of the resulting month.

For example, datetime(2000,9,30)+calquarters(1,ArithmeticMethod="default") returns a datetime value of "30-Dec-2000".

"endofquarter"

In datetime arithmetic, preserve quarter-end dates for datetime values that occur on the last day of a quarter. For all other datetime values, use the same behavior as "default".

For example, datetime(2000,9,30)+calquarters(1,ArithmeticMethod="endofquarter") returns a datetime value of "31-Dec-2000".

Data Types: string | char

Extended Capabilities

expand all

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2014b

expand all