Date and Time Arrays

In MATLAB, you can create date and time arrays and perform comparisons, calculations, and formatted displays.

Creating Date and Time Arrays

1. datetime Function

There are many ways to create arrays representing dates and times using the datetime function.

When called without parameters, the datetime function returns the current date and time.

[Example 2-50] When datetime function

Enter in the Command Window:

code.matlab
T=datetime
T =
   13-Sep-2018 08:08:44

The parameters of the datetime function can be string vectors representing date and time, or you can specify the display format.

[Example 2-51] Use the datetime function to specify the display format.

Enter in the Command Window:

code.matlab
T = datetime({'';''},'InputFormat','yyyy-MM-dd')
T =

You can also write the year, month, and day values as arrays separately, then use the datetime function to create a date and time array.

[Example 2-52] Use the datetime function to create a date and time array.

Enter in the Command Window:

code.matlab
Y = [2016;2017;2018];
M = 06;
D = [11;20;30];
T = datetime(Y,M,D)
T =


2. Creating Duration Arrays

Use the duration function to create duration arrays.

[Example 2-53] Use the duration function to create a duration array.

Enter in the Command Window:

code.matlab
D=duration(6,10:13,5)
D =
   06:10:05   06:11:05   06:12:05   06:13:05

[Example 2-54] You can convert numeric matrix data into a duration array.

Enter in the Command Window:

code.matlab
X=[10 13 45;9 16 38];
D=duration(X)
D =
   10:13:45
   09:16:38

[Example 2-55] Use the years function to create a duration array of fixed-length years.

Enter in the Command Window:

code.matlab
X=[1 2 3;4 5 6];
Y=years(X)
Y =
   1 year   2 years   3 years
   4 years  5 years   6 years

Functions for creating fixed-length duration arrays are shown in Table 2-9.

Table 2-9: Functions for Creating Fixed-Length Duration Arrays

Function Description
years Duration in years.
days Duration in days.
hours Duration in hours.
minutes Duration in minutes.
seconds Duration in seconds.
milliseconds Duration in milliseconds.

Extracting Dates and Times

For a given date and time array, you can use functions to extract time units such as year, month, and day from the array.

[Example 2-56] Use the datetime function to get the current date and time.

Enter in the Command Window:

code.matlab
T=datetime
T =
   13-Sep-2018 13:45:52

[Example 2-57] Get the year and hour numbers.

Enter in the Command Window:

code.matlab
Y=year(T)
Y =
    2018
H=hour(T)
H =
    13

Functions for extracting components by time unit are shown in Table 2-10.

Table 2-10: Functions for Extracting Components by Time Unit

Function Description
year Number of years.
quarter Number of quarters.
month Month number and name.
week Week number.
day Day number and name.
hour Hour number.
minute Minute number.
second Second number.

Display Format

By default, the display format for date and time in MATLAB is: dd-mmm-yyyy hh:mm:ss. For example, 12-Sep-2018 01:10:29 represents September 12, 2018, 1:10:29 AM.

[Example 2-58] Use the yyyymmdd function to convert a date to yyyymmdd format.

Enter in the Command Window:

code.matlab
T=datetime
T =
   13-Sep-2018 13:45:52
T2=yyyymmdd(T)
T2 =
      20180913

[Example 2-59] Use the exceltime function to convert date and time to Excel date numbers. Excel date numbers consist of whole days and fractional days starting from midnight, January 0, 1900.

Enter in the Command Window:

code.matlab
T3=exceltime(T)
T3 =
   4.3357e+04