%% Convert Datetime Array to POSIX Times
% Create datetime values and convert them to the equivalent POSIX® times. Show the differences in POSIX times between zoned and unzoned datetime values. The best practice is to specify a time zone for a datetime array before calling posixtime.
% Create a datetime array and specify its time zone.

t1 = datetime('2016-07-29 10:05:24') + calmonths(1:3);
t1.TimeZone = 'America/New_York'

%
% Convert t1 to the equivalent POSIX times. posixtime accounts for the time zone offset when it computes POSIX times.

format longG
p1 = posixtime(t1)

%
% Create a datetime array with the same values as t1, but with no time zone. Convert it to the equivalent POSIX times. posixtime treats the times in t2 as UTC times, with no time zone offset.

t2 = datetime('2016-07-29 10:05:24') + calmonths(1:3);
p2 = posixtime(t2)

%
% Show the differences between p2 and p1. The differences are equal to the time offset, in seconds, between UTC and the time zone of t1.

p2 - p1

% Copyright 2012 The MathWorks, Inc.