%% Date and Time Arithmetic
% 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('now')

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

t2 = t1 + hours(1:3)

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

dt = diff(t2)

% 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)

%
% 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]

%
%% 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')

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

t2 = t1 + days(0:2)

% 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)

% 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)

%
% 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)

%
%% Add Calendar Durations to Datetime Array
% Add a number of calendar months to January 31, 2014.

t1 = datetime(2014,1,31)

%

t2 = t1 + calmonths(1:4)

% 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')

% 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)

%
% 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')

% 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)

%
% 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)

%
%% Calendar Duration Arithmetic
% Create two calendar durations and then find their sum.

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

%

d2 = calmonths(11) + caldays(23)

%

d = d1 + d2

% 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

%
%% 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)

%

t1 = datetime('yesterday')

%

dt = t2 - t1

%

whos dt

% 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'

%
% 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

%
%% 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('today')

%

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

%

dt = between(t1,t2)

% Copyright 2015 The MathWorks, Inc.