datetime array concat not diplaying time of day when first element is NaT
Show older comments
Consider the following code ( Elements is a Matrix of DataValueElements, a self-defined class with the field DateTimes, an array of Matlab datetimes):
Example 1: (showing content of Elements)
>> {Elements(1:end,4).DateTimes}
ans = 1×4 cell array
[NaT] [01-Mar-2018 10:00:00] [01-Mar-2018 10:00:01] [NaT]
Example 2: (showing problem - time of day not displayed)
>> arr = [Elements(1:4,4).DateTimes]
arr = 1×4 datetime array
NaT 01-Mar-2018 01-Mar-2018 NaT
>> datetime(arr)
ans = 1×4 datetime array
NaT 01-Mar-2018 10:00:00 01-Mar-2018 10:00:01 NaT
Example 3: (problem doesn't occur when first element isn't NaT)
>> [Elements(2:end,4).DateTimes]
ans = 1×3 datetime array
01-Mar-2018 10:00:00 01-Mar-2018 10:00:01 NaT
Why is the time of day not displayed when concatting the array and when the first element is NaT? Or is this a bug?
1 Comment
Jan
on 21 Mar 2018
It would be useful if you provide some code to create the input data. Then the readers can check their ideas before posting.
Answers (2)
Jan
on 21 Mar 2018
This does not reproduce the problem:
c = datetime({'NaT', '01-Mar-2018 10:00:00', '01-Mar-2018 10:00:01', 'NaT'})
But why is it a problem at all? What about defining the Format property of the datetime object explicitly? The automatic detection of the format seems to fail, when the first value is a NaT, but if you want a specific output to the command window, using the magic disp of the ans object is weak at all.
Peter Perkins
on 23 Mar 2018
0 votes
What's going on is that you are concatenating four scalar datetimes, and the display format of the result is coming from the first, and that happens to ba a NaT, and that has been created with a display format that does not include time of day. Not sure where the NaT is coming from, but you can specify a format if you're creating it. If not, you can just adjust the format of your result after concatenating.
This is kind of an unusual case that has hit a rough edge of the format rules for datetime.
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!