How can I convert an epoch value to a 'datetime' value, with millisecond precision?

49 Ansichten (letzte 30 Tage)
How can I transfer an epoch value into real time, with precision to the millisecond?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 11 Mär. 2019
You can transfer an epoch value, 'epVal', to a 'datetime' value with millisecond precision with the following line:
>> d = datetime(epVal, 'convertfrom', 'posixtime', 'Format', 'MM/dd/yy HH:mm:ss.SSS');
For further information about 'datetime' values and their properties, please see the following MATLAB documentation page:
  1 Kommentar
Peter Perkins
Peter Perkins am 25 Mär. 2019
As described in the doc, the value you pass as 'ConvertFrom' for 'posixtime' is
"Number of seconds since 1-Jan-1970 00:00:00 UTC"
So if "with millisecond precision" means "seconds plus fractional seconds", the above is correct. "Seconds plus fractional seconds" is what
>> posixtime(datetime('now'))
ans =
1553518031.88491
returns.
But often ""with millisecond precision"" means "whole milliseconds since 1970", and the above is not the right result. Instead:
>> dnow = datetime('now')
dnow =
datetime
25-Mar-2019 12:48:59
>> et = milliseconds(dnow - datetime(1970,1,1))
et =
1553518139117.12
>> datetime(et,'ConvertFrom','epochtime','TicksPerSecond',1000)
ans =
datetime
25-Mar-2019 12:48:59

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by