%% Convert Date and Time to Julian Date or POSIX Time
% You can convert datetime arrays to represent points in time in specialized numeric formats. In general, these formats represent a point in time as the number of seconds or days that have elapsed since a specified starting point. For example, the Julian date is the number of days and fractional days that have elapsed since the beginning of the Julian period. The POSIX® time is the number of seconds that have elapsed since 00:00:00 1-Jan-1970 UTC (Universal Coordinated Time). MATLAB® provides the juliandate and posixtime functions to convert datetime arrays to Julian dates and POSIX times.
% While datetime arrays are not required to have a time zone, converting "unzoned" datetime values to Julian dates or POSIX times can lead to unexpected results. To ensure the expected result, specify the time zone before conversion.
%% Specify Time Zone Before Conversion
% You can specify a time zone for a datetime array, but you are not required to do so. In fact, by default the datetime function creates an "unzoned" datetime array.
% Create a datetime value for the current date and time.

d = datetime('now')

%
% d is constructed from the local time on your machine and has no time zone associated with it. In many contexts, you might assume that you can treat the times in an unzoned datetime array as local times. However, the juliandate and posixtime functions treat the times in unzoned datetime arrays as UTC times, not local times. To avoid any ambiguity, it is recommended that you avoid using juliandate and posixtime on unzoned datetime arrays. For example, avoid using posixtime(datetime('now')) in your code.
% If your datetime array has values that do not represent UTC times, specify the time zone using the TimeZone name-value pair argument so that juliandate and posixtime interpret the datetime values correctly.

d = datetime('now','TimeZone','America/New_York')

%
% As an alternative, you can specify the TimeZone property after you create the array.

d.TimeZone = 'America/Los_Angeles'

% To see a complete list of time zones, use the timezones function.
%
%% Convert Zoned and Unzoned Datetime Values to Julian Dates
% A Julian date is the number of days (including fractional days) since noon on November 24, 4714 BCE, in the proleptic Gregorian calendar, or January 1, 4713 BCE, in the proleptic Julian calendar. To convert datetime arrays to Julian dates, use the juliandate function.
% Create a datetime array and specify its time zone.

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

%
% Convert D to the equivalent Julian dates.

format longG
JDZ = juliandate(DZ)

%
% Create an unzoned copy of DZ. Convert D to the equivalent Julian dates. As D has no time zone, juliandate treats the times as UTC times.

D = DZ;
D.TimeZone = '';
JD = juliandate(D)

%
% Compare JDZ and JD. The differences are equal to the time zone offset between UTC and the America/New_York time zone in fractional days.

JDZ - JD 

%% Convert Zoned and Unzoned Datetime Values to POSIX Times
% The POSIX time is the number of seconds (including fractional seconds) elapsed since 00:00:00 1-Jan-1970 UTC (Universal Coordinated Time), ignoring leap seconds. To convert datetime arrays to POSIX times, use the posixtime function.
% Create a datetime array and specify its time zone.

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

%
% Convert D to the equivalent POSIX times.

PTZ = posixtime(DZ)

%
% Create an unzoned copy of DZ. Convert D to the equivalent POSIX times. As D has no time zone, posixtime treats the times as UTC times.

D = DZ;
D.TimeZone = '';
PT = posixtime(D)

%
% Compare PTZ and PT. The differences are equal to the time zone offset between UTC and the America/New_York time zone in seconds.

PTZ - PT

% Copyright 2012 The MathWorks, Inc.