convert date string

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 Comments

Jan
Jan on 19 May 2012
What is an "array of strings"? A CHAR matrix or a cell string?
What is the wanted output for the posted input? "[day, time]" is *not* a "serial format".
I never see from this OP any activity except from posting the question. It's like she/he loses interest in the question right after asking.
Trader
Trader on 20 May 2012
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 on 20 May 2012
@Trader: It is a good idea to provide feedback in the forum, especially if the replies are important for you.

Sign in to comment.

Answers (1)

Jan
Jan on 19 May 2012
Edited: Jan on 6 Feb 2018
[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 Comments

Hi, it seems not working. I have a string array:
S = {'20180201 15:06:32.279', '20180201 15:06:32.379'};
Why it's not working? Thanks Michela
Peter Perkins
Peter Perkins on 6 Feb 2018
Edited: Jan on 6 Feb 2018
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 on 6 Feb 2018
Edited: Jan on 6 Feb 2018
@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

Sign in to comment.

Categories

Tags

Asked:

on 19 May 2012

Edited:

Jan
on 6 Feb 2018

Community Treasure Hunt

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

Start Hunting!