Main Content


Convert MATLAB datetime to POSIX time



p = posixtime(t) returns POSIX® times equivalent to the datetime values in t. 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. p is a double array.

  • If the time zone of t is not specified, then posixtime treats the times in t as UTC times. This interpretation might differ from your treatment of “unzoned” datetime arrays in other contexts. For example, you might think of datetime('now') as returning your local time. However, posixtime interprets it as a UTC time.

  • If the time zone of t is specified, then posixtime uses the offset for the time zone to compute POSIX times with respect to UTC.

The best practice is to specify the time zone of t before calling posixtime.


collapse all

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'
t1 = 1x3 datetime
   29-Aug-2016 10:05:24   29-Sep-2016 10:05:24   29-Oct-2016 10:05:24

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

format longG
p1 = posixtime(t1)
p1 = 1×3

    1.4725    1.4752    1.4777

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)
p2 = 1×3

    1.4725    1.4751    1.4777

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
ans = 1×3

      -14400      -14400      -14400

Input Arguments

collapse all

Input date and time, specified as a datetime array.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2014b