MATLAB - Dates and Time



Matlab provides a rich source of commands to deal with date and time. You will be able to display the date and time in different formats either as string or character vector.Also you can perform math operations like add, subtract, sort, concatenate and plotting of graphs with date and time values.

Here are a few commands with examples to get the current date and time in Matlab. The array of date and time can be displayed in different formats and over here we are going to discuss the different methods in matlab that represent it.

Using date command

The date command returns the current date as character vector in the format dd-MMM-yyyy.This format represents the day of the month (dd) as a number, the month name (MMM) as its three-letter abbreviation, and the year (yyyy) as a number.The date returned will not consider local time but instead it will take current date from Coordinated Universal Time (UTC).

Syntax

date

Example

a = date

When you execute the command in matlab output will be −

a = 30-Aug-2024

Using datetime command

The datetime command returns a date with time that represents points in time.

  • datetime − Returns an array that will have current date and time.
  • datetime(relativeDay) − Here relativeDay can have inputs as today, yesterday,tomorrow, or now.
  • datetime(DateStrings) − Here DateStrings can be date and time in array format.
  • datetime(DateVectors) − Date vector is a matrix of m-by-6 or m-by-3. With m-by-6 it will have day,month,year , hour ,minute and seconds. With my-by-3 it will be a matrix with day, month and year.
  • datetime(Y,M,D) − Datetime with year, month and day.
  • datetime(Y,M,D,H,MI,S) − Datetime with year, month , day, hour , minute and seconds.

Example 1

Just using datetime() method will display the current date and time as shown below −

k = datetime

On execution the output is as follows −

>> k = datetime

k = 

   datetime

   24-May-2023 07:56:32

>>

Example 2

This example will display the time in the local timezone.

time_now = datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss Z')

When you execute the same in matlab command window the output is −

>> time_now = datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss Z')

time_now = 

   datetime

   24-May-2023 07:48:50 +0530

>>

The date and time in the America/New York timezone.

>>  time_now = datetime('now','TimeZone','America/New_York','Format','d-MMM-y HH:mm:ss Z')

time_now = 

   datetime

   23-May-2023 22:23:01 -0400

>> 

Example 3

In this example we will take datestrings as vector arrays.

DateasStrings = {'2023-05-22';'2023-05-23'};
t = datetime(DateasStrings)

On execution you will get −

>>  DateasStrings = {'2023-05-22';'2023-05-23'};
t = datetime(DateasStrings)


t = 

   21 datetime array

   22-May-2023
   23-May-2023

>>  

You can also convert the array given into a format you want as shown below

DateStrings = {'2022-05-26';'2022-08-03'};
t = datetime(DateStrings, 'InputFormat','yyyy-MM-dd')

In above above the dates will be converted to format yyyy-MM-dd , on execution the output is as follows −

>> DateStrings = {'2022-05-26';'2022-08-03'};
t = datetime(DateStrings ,'InputFormat','yyyy-MM-dd')


t = 

   21 datetime array

   26-May-2022
   03-Aug-2022

>> 

Example 4

As input you can give year , month and day to the datetime and it will give you the date back.

datetime(2023, 5, 12)

The output for above is −

>> datetime(2023, 5, 12)

ans = 

   datetime

   12-May-2023

>> 

Example 5

You can give input with year , month , day, hour, minute and seconds as shown below −

datetime(2023,5,24,8,21,5)

On execution the output is −

>> datetime(2023,5,24,8,21,5)

ans = 

   datetime

   24-May-2023 08:21:05

>> 

Using Calendar Methods

This method will give you the calendar for the given year month. By default it will return a calendar for the current year month.

Syntax

calendar  // current year and month will be displayed.

calendar(y,m)  // the given year month will be displayed

calendar(d)  // Here d datetime value or text timestamp that can be year month and day .As per the value of d , the calendar for that month and year will be displayed.

Example 1

Let us check the output of the calendar.

calendar

The output is −

                   May 2023
     S     M    Tu     W    Th     F     S
     0     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    31     0     0     0
     0     0     0     0     0     0     0

Example 2

In this example the year and month will be passed to the calendar.

calendar(1983, 2)

The output is −

                   Feb 1983
     S     M    Tu     W    Th     F     S
     0     0     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     0     0     0     0     0
     0     0     0     0     0     0     0

Example 3

In this example will use datetime as a input to calendar as shown below −

d = datetime("now")
calendar(d)

On execution the output is −

>> d = datetime("now")
calendar(d)


d = 

   datetime

   24-May-2023 08:44:57

                   May 2023
     S     M    Tu     W    Th     F     S
     0     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    31     0     0     0
     0     0     0     0     0     0     0

>>

Shifting of Date Time Using dateshift()

The dateshift() method takes care of shifting dates or generates the sequence of date and time.

Syntax

t = dateshift(t,'start',unit)   // Here t is the datetime.It will shift the datetime by the unit given to the start.

t = dateshift(t,'end',unit) // Here t is the datetime. It will shift the datetime by the given unit to the end.


t = dateshift(t,'dayofweek',dow) // Here datetime will be shifted to the given dayofweek, if the current date falls on the same dayofweek it will return the same date.

Here will try a few examples like to shift the current date to the start of the year.Shifting of current date to end of the month.

Example 1

In this example the current date May 24 2023 will shift to the start of the year that is 01 Jan 2023.

t = datetime('today')
t1 = dateshift(t, 'start', 'year')

On execution the output is −

>> t = datetime('today')
t1 = dateshift(t, 'start', 'year')


t = 

   datetime

   24-May-2023


t1 = 

   datetime

   01-Jan-2023

>>

Example 2

In this example will shift the current date to the end of the month.

t = datetime('today')
t1 = dateshift(t, 'end', 'month')

On execution the output is −

>> t = datetime('today')
t1 = dateshift(t, 'end', 'month')


t = 

  datetime

   24-May-2023


t1 = 

  datetime

   31-May-2023

>>

Also we can shift it to the end of the year by using the unit as year.

t = datetime('today')
t1 = dateshift(t, 'end', 'year')

The output is

>> t = datetime('today')
t1 = dateshift(t, 'end', 'year')


t = 

  datetime

   24-May-2023


t1 = 

  datetime

   31-Dec-2023

>> 

Arithmetic Operation on Date and Time

We will discuss arithmetic operations like add, subtract on date and time. By adding we are going to get future date time, and subtract to get the past date time. The operation like add, subtract , multiply etc can be done on date and time same as it is done on other data types. You can make use of hours() ,minutes(), days(), seconds() , years() to add/subtract from a given date time.

Let us see some examples.

Example 1

In this example we are going to use the datetime() method that will display current date and time. We are going to add 2 hours to it and display the date time as shown below.

time1 = datetime('now')
time2 = time1 + hours(2)

On execution in Matlab the output is as follows −

>> time1 = datetime('now')
time2 = time1 + hours(2)


time1 = 

  datetime

   17-Jun-2023 17:17:52


time2 = 

  datetime

   17-Jun-2023 19:17:52

>>

Incase you want future dates and time and display the array of vector it can be done as follows −

time1 = datetime('now')
time2 = time1 + hours(2:5)

On execution the output is −

>> time1 = datetime('now')
time2 = time1 + hours(2:5)


time1 = 

  datetime

   17-Jun-2023 17:18:36


time2 = 

  1×4 datetime array

   17-Jun-2023 19:18:36   17-Jun-2023 20:18:36   17-Jun-2023 21:18:36   17-Jun-2023 22:18:36

>>

In the above example you can see the vector array of date time and the time duration differs by 2 hours between them.

Example 2

In this example let us perform a subtract operation as shown below.

time1 = datetime('now')
time2 = time1 - hours(2)

The datetime() method is used to give current date and time. From the given time 2 hours are subtracted. The hours(2) helps to give hours , similarly you can use the minutes() method to deduct minutes from the given time.

>> time1 = datetime('now')
time2 = time1 - hours(2)

time1 = 

  datetime

   17-Jun-2023 17:22:27


time2 = 

  datetime

   17-Jun-2023 15:22:27

>> 

Let us also make use of the minutes() method as shown below.

time1 = datetime('now')
time2 = time1 - minutes(45)

On execution the output is as follows −

>> time1 = datetime('now')
time2 = time1 - minutes(45)


time1 = 

  datetime

   17-Jun-2023 17:26:54


time2 = 

  datetime

   17-Jun-2023 16:41:54

>> 

Let us also try to get a vector array using subtract operation as shown below

time1 = datetime('now')
time2 = time1 - hours(2:5)

On execution the output is as follows −

>> time1 = datetime('now')
time2 = time1 - hours(2:5)


time1 = 

  datetime

   17-Jun-2023 17:32:04


time2 = 

  14 datetime array

   17-Jun-2023 15:32:04   17-Jun-2023 14:32:04   17-Jun-2023 13:32:04   17-Jun-2023 12:32:04

>> 

Example 3

In the examples above we have used hours() , minutes() , datetime() methods to add/subtract. Now let us make use of days() methods, to get future or past dates.

time1 = datetime('now')
time2 = time1 + days(5)

In the above case 5 days will be added to the current date and the date time will be displayed.

On execution the output is as follows −

>> time1 = datetime('now')
time2 = time1 + days(5)


time1 = 

  datetime

   17-Jun-2023 17:42:42


time2 = 

  datetime

   22-Jun-2023 17:42:42

>> 

Let us also subtract and get the past date time using the days() method.

time1 = datetime('now')
time2 = time1 - days(5)

On execution the output is as follows −

>> time1 = datetime('now')
time2 = time1 - days(5)


time1 = 

  datetime

   17-Jun-2023 17:45:19


time2 = 

  datetime

   12-Jun-2023 17:45:19

>> 
Advertisements