Date and Time Arithmetic - MATLAB & Simulink (original) (raw)

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.

Add and Subtract Durations to Datetime Array

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

t1 = datetime 01-Feb-2025 08:59:37

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

t2 = 1×3 datetime 01-Feb-2025 09:59:37 01-Feb-2025 10:59:37 01-Feb-2025 11:59:37

Verify that the difference between each pair of datetime values in t2 is 1 hour.

dt = 1×2 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 = 1×3 datetime 01-Feb-2025 08:39:37 01-Feb-2025 08:29:37 01-Feb-2025 08:19:37

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 = 1×3 datetime 02-Feb-2025 08:59:37 03-Feb-2025 08:59:37 04-Feb-2025 08:59:37

Add to Datetime with Time Zone

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 = 1×3 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 = 1×2 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 = 1×3 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 = 1×2 duration 24:00:00 23:00:00

Add Calendar Durations to Datetime Array

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

t1 = datetime 31-Jan-2014

t2 = 1×4 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 = 1×3 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 = 1×5 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 = 1×4 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

Calendar Duration Arithmetic

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 = 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.

ans = calendarDuration 4y 2mo 86d

Calculate Elapsed Time in Exact Units

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 = 1×3 datetime 02-Feb-2025 08:59:38 03-Feb-2025 08:59:38 04-Feb-2025 08:59:38

t1 = datetime('yesterday')

t1 = datetime 31-Jan-2025

dt = 1×3 duration 56:59:38 80:59:38 104:59:38

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 = 1×3 duration 2.3747 days 3.3747 days 4.3747 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×3 duration 2.8497 days 4.0497 days 5.2497 days

Calculate Elapsed Time in Calendar Units

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

t1 = datetime 01-Feb-2025

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

t2 = 1×3 datetime 05-Feb-2025 05-Mar-2025 05-Apr-2025

dt = 1×3 calendarDuration 4d 1mo 4d 2mo 4d

See Also

between | diff | caldiff