필터 지우기
필터 지우기

convert date string

조회 수: 4 (최근 30일)
Trader
Trader 2012년 5월 19일
편집: Jan 2018년 2월 6일
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
Trader
Trader 2012년 5월 20일
Jan - I apologize, I should have said cell array filled with strings. Thanks for your response.
Oleg - I don't lose interest, I've got a extremely tight deadline right now so I haven't had much time to contribute back to the board. Everyone has been very helpful and I do appreciate their responses.
Jan
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
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
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
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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by