%% Elapsed Times with DST Shift
% Calculate elapsed times since midnight on a day with a Daylight Saving Time (DST) shift.
% Create a datetime array. Set its TimeZone property to a time zone that observes DST. Set the date to a day when a DST shift occurred.

tz = 'America/New_York';
fmt = 'dd-MMM-yyyy HH:mm:ss z';
DT = datetime(2015,3,8,'TimeZone',tz,'Format',fmt) + hours(1:4)

%
% Calculate the elapsed times. The DST shift occurred at 02:00 on March 8, 2015 in this time zone. timeofday accounts for the shift for times at or after 02:00 on this date. Set the format of T to display elapsed times in hours.

T = timeofday(DT);
T.Format = 'h'

%
% For comparison, calculate elapsed times using the hour, minute, and second components of DT. This calculation does not account for the DST shift.

E = hours(DT.Hour) + minutes(DT.Minute) + seconds(DT.Second)

% Copyright 2015-2021 The MathWorks, Inc.