Why won't datetime work for 0:00?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Seattle
am 9 Jun. 2023
Kommentiert: Peter Perkins
am 17 Jul. 2023
I have this code to help format a list of dates/times in a CSV file:
datfmt = '%s%s%s%s%f%s%s%s%d%s%f%s';
celldata = textscan(fid,datfmt,'Headerlines',1,'Delimiter',',');
dates = datetime(celldata{3});
When I try to write an output file using:
for iline = 1:length(dates)
fprintf(fid,'%s\n',dates(iline));
end
the dates/times at the 0:00 mark don't read out with both the dates/times, only the dates (formatted differently than all the other values too). Why is this happening?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 9 Jun. 2023
datetime('01/31/1983'), ans.Format
datetime('01/31/1983 00:00'), ans.Format
datetime('01/31/1983 17:45'), ans.Format
What is happening is that when you do not supply a Format, the default format for time 00:00 is just the date without the time. If you want the time as well, specify a format specifically
datetime('01/31/1983 00:00', 'Format', 'dd-MMM-uuuu HH:mm:ss'), ans.Format
1 Kommentar
Peter Perkins
am 17 Jul. 2023
Walter is right, but even MORE right would be to say, "CSV? use readtable, and tell it to read those timestamps directly into a datetime variable." textscan is not your friend.
Weitere Antworten (1)
the cyclist
am 9 Jun. 2023
It's not easy to debug this without seeing the data, but I would suggest explicitly adding a 'Format' Name-Value input to this line
dates = datetime(celldata{3});
so that that those dates are forced to include the time.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!