Main Content

This example shows how to add and subtract date and time values to calculate future and past dates and elapsed durations in exact units or calendar units. You can add, subtract, multiply, and divide date and time arrays in the same way that you use these operators with other MATLAB® data types. However, there is some behavior that is specific to dates and time.

Create a datetime scalar. By default, datetime arrays are not associated with a time zone.

`t1 = datetime('now')`

`t1 = `*datetime*
23-Feb-2021 14:57:33

Find future points in time by adding a sequence of hours.

t2 = t1 + hours(1:3)

`t2 = `*1x3 datetime*
23-Feb-2021 15:57:33 23-Feb-2021 16:57:33 23-Feb-2021 17:57:33

Verify that the difference between each pair of datetime values in `t2`

is 1 hour.

dt = diff(t2)

`dt = `*1x2 duration*
01:00:00 01:00:00

`diff`

returns durations in terms of exact numbers of hours, minutes, and seconds.

Subtract a sequence of minutes from a datetime to find past points in time.

t2 = t1 - minutes(20:10:40)

`t2 = `*1x3 datetime*
23-Feb-2021 14:37:33 23-Feb-2021 14:27:33 23-Feb-2021 14:17:33

Add a numeric array to a `datetime`

array. MATLAB® treats each value in the numeric array as a number of exact, 24-hour days.

t2 = t1 + [1:3]

`t2 = `*1x3 datetime*
24-Feb-2021 14:57:33 25-Feb-2021 14:57:33 26-Feb-2021 14:57:33

If you work with datetime values in different time zones, or if you want to account for daylight saving time changes, work with datetime arrays that are associated with time zones. Create a `datetime`

scalar representing March 8, 2014 in New York.

t1 = datetime(2014,3,8,0,0,0,'TimeZone','America/New_York')

`t1 = `*datetime*
08-Mar-2014

Find future points in time by adding a sequence of fixed-length (24-hour) days.

t2 = t1 + days(0:2)

`t2 = `*1x3 datetime*
08-Mar-2014 00:00:00 09-Mar-2014 00:00:00 10-Mar-2014 01:00:00

Because a daylight saving time shift occurred on March 9, 2014, the third datetime in `t2`

does not occur at midnight.

Verify that the difference between each pair of datetime values in `t2`

is 24 hours.

dt = diff(t2)

`dt = `*1x2 duration*
24:00:00 24:00:00

You can add fixed-length durations in other units such as years, hours, minutes, and seconds by adding the outputs of the `years`

, `hours`

, `minutes`

, and `seconds`

functions, respectively.

To account for daylight saving time changes, you should work with calendar durations instead of durations. Calendar durations account for daylight saving time shifts when they are added to or subtracted from datetime values.

Add a number of calendar days to `t1`

.

t3 = t1 + caldays(0:2)

`t3 = `*1x3 datetime*
08-Mar-2014 09-Mar-2014 10-Mar-2014

View that the difference between each pair of datetime values in `t3`

is not always 24 hours due to the daylight saving time shift that occurred on March 9.

dt = diff(t3)

`dt = `*1x2 duration*
24:00:00 23:00:00

Add a number of calendar months to January 31, 2014.

t1 = datetime(2014,1,31)

`t1 = `*datetime*
31-Jan-2014

t2 = t1 + calmonths(1:4)

`t2 = `*1x4 datetime*
28-Feb-2014 31-Mar-2014 30-Apr-2014 31-May-2014

Each datetime in `t2`

occurs on the last day of each month.

Calculate the difference between each pair of datetime values in `t2`

in terms of a number of calendar days using the `caldiff`

function.

`dt = caldiff(t2,'days')`

`dt = `*1x3 calendarDuration*
31d 30d 31d

The number of days between successive pairs of datetime values in `dt`

is not always the same because different months consist of a different number of days.

Add a number of calendar years to January 31, 2014.

t2 = t1 + calyears(0:4)

`t2 = `*1x5 datetime*
31-Jan-2014 31-Jan-2015 31-Jan-2016 31-Jan-2017 31-Jan-2018

Calculate the difference between each pair of datetime values in `t2`

in terms of a number of calendar days using the `caldiff`

function.

`dt = caldiff(t2,'days')`

`dt = `*1x4 calendarDuration*
365d 365d 366d 365d

The number of days between successive pairs of datetime values in `dt`

is not always the same because 2016 is a leap year and has 366 days.

You can use the `calquarters`

, `calweeks`

, and `caldays`

functions to create arrays of calendar quarters, calendar weeks, or calendar days that you add to or subtract from datetime arrays.

Adding calendar durations is not commutative. When you add more than one `calendarDuration`

array to a datetime, MATLAB® adds them in the order in which they appear in the command.

Add 3 calendar months followed by 30 calendar days to January 31, 2014.

t2 = datetime(2014,1,31) + calmonths(3) + caldays(30)

`t2 = `*datetime*
30-May-2014

First add 30 calendar days to the same date, and then add 3 calendar months. The result is not the same because when you add a calendar duration to a datetime, the number of days added depends on the original date.

t2 = datetime(2014,1,31) + caldays(30) + calmonths(3)

`t2 = `*datetime*
02-Jun-2014

Create two calendar durations and then find their sum.

d1 = calyears(1) + calmonths(2) + caldays(20)

`d1 = `*calendarDuration*
1y 2mo 20d

d2 = calmonths(11) + caldays(23)

`d2 = `*calendarDuration*
11mo 23d

d = d1 + d2

`d = `*calendarDuration*
2y 1mo 43d

When you sum two or more calendar durations, a number of months greater than 12 roll over to a number of years. However, a large number of days does not roll over to a number of months, because different months consist of different numbers of days.

Increase `d`

by multiplying it by a factor of 2. Calendar duration values must be integers, so you can multiply them only by integer values.

2*d

`ans = `*calendarDuration*
4y 2mo 86d

Subtract one `datetime`

array from another to calculate elapsed time in terms of an exact number of hours, minutes, and seconds.

Find the exact length of time between a sequence of datetime values and the start of the previous day.

`t2 = datetime('now') + caldays(1:3)`

`t2 = `*1x3 datetime*
24-Feb-2021 14:57:34 25-Feb-2021 14:57:34 26-Feb-2021 14:57:34

`t1 = datetime('yesterday')`

`t1 = `*datetime*
22-Feb-2021

dt = t2 - t1

`dt = `*1x3 duration*
62:57:34 86:57:34 110:57:34

`whos dt`

Name Size Bytes Class Attributes dt 1x3 40 duration

`dt`

contains durations in the format, hours:minutes:seconds.

View the elapsed durations in units of days by changing the `Format`

property of `dt`

.

`dt.Format = 'd'`

`dt = `*1x3 duration*
2.6233 days 3.6233 days 4.6233 days

Scale the duration values by multiplying `dt`

by a factor of 1.2. Because durations have an exact length, you can multiply and divide them by fractional values.

dt2 = 1.2*dt

`dt2 = `*1x3 duration*
3.148 days 4.348 days 5.548 days

Use the `between`

function to find the number of calendar years, months, and days elapsed between two dates.

`t1 = datetime('today')`

`t1 = `*datetime*
23-Feb-2021

t2 = t1 + calmonths(0:2) + caldays(4)

`t2 = `*1x3 datetime*
27-Feb-2021 27-Mar-2021 27-Apr-2021

dt = between(t1,t2)

`dt = `*1x3 calendarDuration*
4d 1mo 4d 2mo 4d