Working with date and time in MATLAB is a primary component that can be stored in an array in the form of points. These points represent the year, month, day, hour, minute, and second respectively using the proleptic ISO calendar. This array supports arithmetic operations, comparison, sorting, and formatted display. We can create a datetime array in MATLAB using the built-in **datetime() **function.

Follow this blog to learn how to work with date and time in MATLAB using the **datetime()** function.

## How to Work with Dates and Time in MATLAB Using datetime()

As we discussed earlier, MATLAB supports the creation of a datetime array using the** datetime()** function. This function takes some mandatory and optional arguments and creates an array containing date and time respectively.

**Syntax**

In MATLAB, you can use the **datetime()** function in the following ways:

t = datetime(relativeDay)

t = datetime(DateStrings)

t = datetime(DateVectors)

t = datetime(Y,M,D)

t = datetime(Y,M,D,H,MI,S)

Here,

The function **t=datetime** generates a scalar array containing the current date and time.

The function **t = datetime(relativeDay) **provides the date corresponding to the relativeDay variable that can have values **“today”, “now”, “yesterday”, “ tomorrow”,** and so on.

The function **t = datetime(DateStrings) **creates a datetime array from the text in the DateStrings variable containing the date and time in the form of points.

The function **t = datetime(DateVectors) **creates a column vector containing datetime values corresponding to the value in DateVectors.

The function **t = datetime(Y,M,D) **creates a datetime array corresponding to the values Y, M, D in the input array. The input parameters can have a scalar or an array form.

The function **t = datetime(Y,M,D, H,MI,S) **creates a datetime array corresponding to the values Y, M, D,H,MI,S in the input array; these input parameters can have a scalar or an array form.

**Example 1: Creation of a Datetime Array Containing Current Date and Time**

This MATLAB code generates a datetime array containing the current date and time in the format of D-M-Y-H-MI-S.

**Example 2: Creation of a Datetime Array Containing Tomorrow Date**

This example creates a datetime array containing the tomorrow date in the format of D-M-Y.

**Example 3: Creation of a Datetime Array from the DateStrings**

In this example, we create a datetime array from a string of characters.

t = datetime(DateStrings)

**Example 4: Creation of a Datetime Array from Column Vectors**

The given MATLAB code creates a datetime array from the given vectors.

M = [07;09;06;11];

D = [25;11;29;04];

t = datetime(Y,M,D)

**Example 5: Creation of a Datetime Array from Column Vectors in the Format D-M-Y-H-MI-S**

In this MATLAB code, we create a datetime array from the given column vectors in the format of D-M-Y-H-MI-S.

M = [07;09;06;11];

D = [25;11;29;04];

H = [10;10;12;04];

MI= [05;10;30;15];

S = [24;54;03;10];

t = datetime(Y,M,D,H,MI,S)

## Conclusion

We can create an array of datatype datetime in MATLAB containing the date and time in the form of points using the built-in **datetime()** function. This array allows us to perform arithmetic operations, comparison, sorting, and many other operations. This guide has presented the implementation of different syntaxes of the **datetime() **function with examples, helping us to work with date and time in MATLAB.