MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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 = datetime
13-Apr-2019 02:51:59

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

7 Dates and Time

Free download pdf