# Different Dates And Times Commands In MatLab

Matlab® provides various kinds of commands to show dates and times in various ways. Check all example codes executed in the Matlab® command view to understand all the codes that show dates and times.

On this post

## Date And Time Commands In MatLab®

### clock Command In MatLab®

‘clock’ is a very useful command of Matlab® that you can directly type inside the command window like below.

``````>> clock

ans =

1.0e+03 *

2.0200    0.0120    0.0220    0.0140    0.0240    0.0176``````

In answer, you can see the exact execution time. From left to right, the clock command shows; year, month, day, hour, minute, and second. Consider the multiplication factor indicated right above the answer which is 1.0e+03.

### date Command In MatLab®

``````>> date

ans =

'22-Dec-2020'

>> ``````

If you want to obtain the exact date as an integer in Matlab®, you can use the ‘date’ command in the command window as you see in the example above.

### date2str() Command In MatLab®

``````
>> a = date
datestr(a,2)
datestr(a,3)
datestr(a,4)
datestr(a,5)
datestr(a,6)
datestr(a,7)
datestr(a,8)
datestr(a,9)
datestr(a,10)
datestr(a,11)
datestr(a,12)
datestr(a,13)
datestr(a,14)
datestr(a,15)
datestr(a,16)
datestr(a,17)
datestr(a,18)
datestr(a,19)
datestr(a,20)
datestr(a,21)
datestr(a,22)
datestr(a,23)
datestr(a,24)
datestr(a,25)
datestr(a,26)
datestr(a,27)
datestr(a,28)
datestr(a,29)
datestr(a,30)
datestr(a,31)

a =

'22-Dec-2020'

ans =

'12/22/20'

ans =

'Dec'

ans =

'D'

ans =

'12'

ans =

'12/22'

ans =

'22'

ans =

'Tue'

ans =

'T'

ans =

'2020'

ans =

'20'

ans =

'Dec20'

ans =

'00:00:00'

ans =

'12:00:00 AM'

ans =

'00:00'

ans =

'12:00 AM'

ans =

'Q4-20'

ans =

'Q4'

ans =

'22/12'

ans =

'22/12/20'

ans =

'Dec.22,2020 00:00:00'

ans =

'Dec.22,2020'

ans =

'12/22/2020'

ans =

'22/12/2020'

ans =

'20/12/22'

ans =

'2020/12/22'

ans =

'Q4-2020'

ans =

'Dec2020'

ans =

'2020-12-22'

ans =

'20201222T000000'

ans =

'2020-12-22 00:00:00'

>> ``````

This is a very long example of code that executed in Matlab®. We assigned the date command to variable ‘a’, then we typed the ‘a’ inside ‘date2str’ command. Various representation modes of the date2str() command are shown individually. You just need to type the date data inside the brackets of the ‘date2str()’ command and type the mode that you want to see the date in the command window.

There are 31 types of modes that are available as you see in the example.

### now Command In Matlab®

``````>> now

ans =

7.3815e+05``````

Now command gives the exact time serial date value inside the command window in Matlab®.

### datenum() Command In MatLab®

``````>> datenum(2021, 1, 20, 10, 4, 32)

ans =

7.3818e+05``````

If you enter the exact time inside the brackets of the ‘datenum()’ command in Matlab®, it will give the serial date value in the answer. You need to enter the time starting from left; year, month, day, hour, minute, and second.

YOU CAN LEARN MatLab® IN MECHANICAL BASE; Click And Start To Learn MatLab!

### weekday() Command In MatLab®

``````>> [x, day] = weekday('20/10/2019')

x =

7

day =

'Sat'``````

To use the weekday() command in the Matlab® command window, you just need to enter the exact date that you want in the form above. Enter the date inside brackets and quotes. Assign two variables as above ‘x’ and ‘day’ which will be answered. Put these variables inside square brackets.

The first answer assigned to the first variable is the number of the week inside the defined year. And the second answer that is assigned to the second variable is the day of the week. It is given as a string.

### calendar() Command In MatLab®

``````>> calendar('25/04/1997')
Sep 0030
S     M    Tu     W    Th     F     S
1     2     3     4     5     6     7
8     9    10    11    12    13    14
15    16    17    18    19    20    21
22    23    24    25    26    27    28
29    30     0     0     0     0     0
0     0     0     0     0     0     0

>> ``````

The use of the calendar() command in the Matlab® command window is also very easy. Just enter the date inside the brackets in the form like above. Inside the quotes also. The exact calendar of the related month for this date will be given as a table in the command window of MatLab®.

## Conclusion

As you see above, the use of date and time commands is very easy in Matlab®. If you know further commands, please leave your knowledge in the comments below to share with everyone.

If you want further examples about the date and time commands in Matlab®, inform us in the comments below.

Do not forget to leave your comments and questions below about date and time commands in Matlab® below.

Your precious feedbacks are very important to us.