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

57 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 11 Mar 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 Comment
Peter Perkins
Peter Perkins on 25 Mar 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

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!