MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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 array
14-Apr-2019 02:46:19 15-Apr-2019 02:46:19 16-Apr-2019 02:46:19


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 00:00:00


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


t2 = t1 + days(0:2)


t2 = 1x3 datetime array
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 array
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.


Date and Time Arithmetic
Free download pdf