Main Content

caldays

Calendar duration in days

Description

The caldays function returns an array that represents calendar days. 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. To create exact fixed-length 24-hour days instead, use the days function.

D = caldays(X) returns an array representing calendar days equivalent to the values in array X.

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

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

example

Examples

collapse all

X = magic(5);
D = caldays(X)
D = 5x5 calendarDuration
   17d   24d    1d    8d   15d
   23d    5d    7d   14d   16d
    4d    6d   13d   20d   22d
   10d   12d   19d   21d    3d
   11d   18d   25d    2d    9d

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

X = caldays(8:10) + hours(1.2345)
X = 1x3 calendarDuration
    8d 1h 14m 4.2s    9d 1h 14m 4.2s   10d 1h 14m 4.2s

D = caldays(X)
D = 1×3

     8     9    10

caldays returns a numeric array.

Add two calendar days to the current date and time.

t = datetime('now') + caldays(2)
t = datetime
   07-Sep-2024 15:32:43

Create a sequence of consecutive dates beginning on March 18, 2014.

T = datetime([2014,03,18]) + caldays(0:4)
T = 1x5 datetime
   18-Mar-2014   19-Mar-2014   20-Mar-2014   21-Mar-2014   22-Mar-2014

Create a sequence of dates beginning on March 18, 2014, spaced 2 days apart.

T = datetime([2014,03,18]) + caldays(0:2:8)
T = 1x5 datetime
   18-Mar-2014   20-Mar-2014   22-Mar-2014   24-Mar-2014   26-Mar-2014

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.

Tips

  • caldays creates days that account for Daylight Saving Time shifts when used in calendar calculations. To create exact fixed-length (24 hour) days, use the days function.

Extended Capabilities

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

Version History

Introduced in R2014b