datetime conversion lose nanoseconds

9 views (last 30 days)
Andrea Sbaragli
Andrea Sbaragli on 21 May 2021
Answered: Steven Lord on 21 May 2021
I have converted an array from epoch time in nanoseconds to datetime using the following:
t(i,1)= datetime(B(i,1),'ConvertFrom','epochtime','TicksPerSecond',1e9,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS');
the coversion is ok but once opened the array somehow the this part SSSSSSSSS is not displayed at all.
At the same time doing it without a function value per value it works...
How can I solve this issue?
  1 Comment
Stephen23
Stephen23 on 21 May 2021
ms = intmax('int64');
dt = datetime(ms,'ConvertFrom','epochtime','TicksPerSecond',1e9,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS')
dt = datetime
11-Apr-2262 23:47:16.854775807

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 21 May 2021
What Format did you use when you assigned the datetime in the first row of t? Was it one that did not include the nanosecond information?
format1 = 'yyyy-MM-dd HH';
t = datetime('now', 'Format', format1)
t = datetime
2021-05-21 18
format2 = 'HH-mm-ss ''on'' yyyy-MM-dd';
t(2, 1) = datetime('now', 'Format', format2)
t = 2×1 datetime array
2021-05-21 18 2021-05-21 18
The whole array uses format1 because that was the format already in use when you assigned to the second row of t. The second element of t was converted to use the existing format. If you change the array's Format both elements are displayed in the new format.
t.Format = format2
t = 2×1 datetime array
18-24-27 on 2021-05-21 18-24-27 on 2021-05-21

Categories

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

Community Treasure Hunt

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

Start Hunting!