convert date string
2 次查看(过去 30 天)
显示 更早的评论
I've got an array of strings that are dates in this format -> '20120518 08:30:00'. I'd like to convert everything into a serial format and store it in an array like dateTime = [day, time];
What would be the most efficient way to do this?
Thanks for your help!
4 个评论
Jan
2012-5-20
@Trader: It is a good idea to provide feedback in the forum, especially if the replies are important for you.
回答(1 个)
Jan
2012-5-19
编辑:Jan
2018-2-6
[EDITED, cell string as input after Trader's comment]
S = {'20120518 08:30:00', '20110519 09:40:00'};
C = sprintf('%s*', S{:});
V = sscanf(C, '%4d%2d%2d %d:%d:%d*'); % [EDITED]
V = reshape(V, 6, []).'; % [EDITED]
SerialDate = datenummx(V);
dateTime = [floor(SerialDate), rem(SerialDate, 1) * 86400];
While Matlab's date functions are very powerful, they are slow. The undocumented datenummx exists since Matlab 5.3 to (at least) 2016b and it is the fast core under the smart and intelligent datenum.
3 个评论
Peter Perkins
2018-2-6
编辑:Jan
2018-2-6
Things have changed in 6 years. Unless you are using a pretty old version of MATLAB, use datetimes:
>> S = {'20180201 15:06:32.279', '20180201 15:06:32.379'};
>> dt = datetime(S,'InputFormat','yyyyMMdd HH:mm:ss.SSS')
dt =
1×2 datetime array
01-Feb-2018 15:06:32.279 01-Feb-2018 15:06:32.379
>> d = dateshift(dt,'start','day'); d.Format = 'dd-MMM-yyy'
d =
1×2 datetime array
01-Feb-2018 01-Feb-2018
>> t = timeofday(dt)
t =
1×2 duration array
15:06:32 15:06:32
Jan
2018-2-6
编辑:Jan
2018-2-6
@Michaela Longhi: You did not reveal what "is not working" means. Do you get an error message or does the result differ from your expectations? Maybe you use a modern Matlab version, which does not include datenummx anymore. Or you copied my code without considering, that it does not accept the fractional seconds. Or you did not fix the obvious typo "sprintfS" to "C" (fixed in the code now).
This works under R2016b and considers fractional seconds also:
S = {'20180201 15:06:32.279', '20180201 15:06:32.379'};
C = sprintf('%s*', S{:});
V = sscanf(C, '%4d%2d%2d %d:%d:%g*');
V = reshape(V, 6, []).';
SerialDate = datenummx(V);
dateTime = [floor(SerialDate), rem(SerialDate, 1) * 86400];
Peter's suggestion is more up-to-date. But see the timings for a longer input:
S1 = {'20180201 15:06:32.279', '20180201 15:06:32.379'};
S = repmat(S, 1, 10000);
tic;
dt = datetime(S, 'InputFormat', 'yyyyMMdd HH:mm:ss.SSS')
d = dateshift(dt, 'start', 'day');
t = timeofday(dt);
toc
tic;
C = sprintf('%s*', S{:});
V = sscanf(C, '%4d%2d%2d %d:%d:%g*');
V = reshape(V, 6, []).';
SerialDate = datenummx(V);
dateTime = [floor(SerialDate), rem(SerialDate, 1) * 86400];
toc
Under R2016b:
Elapsed time is 1.019361 seconds. % datetime
Elapsed time is 0.057058 seconds. % old serial date format
In addition the output of the modern method are datetime objects. This might be useful, but is not necessarily what the original poster meant by "[day, time]".
The old methods to work with the serial date numbers have been slow already and some less intelligent conversion methods could be 100 times faster (see FEX: DateConvert and FEX: DateStr2Num). Example: Converting a cell string of 10'000 date strings 'dd-mmm-yyyy HH:MM:SS' to the serial date numbers, e.g. for sorting files:
0.272 sec datenum
0.0014 sec DateStr2Num.mex
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!