convert date string
    3 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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 commentaires
  Jan
      
      
 le 20 Mai 2012
				@Trader: It is a good idea to provide feedback in the forum, especially if the replies are important for you.
Réponses (1)
  Jan
      
      
 le 19 Mai 2012
        
      Modifié(e) : Jan
      
      
 le 6 Fév 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 commentaires
  Peter Perkins
    
 le 6 Fév 2018
				
      Modifié(e) : Jan
      
      
 le 6 Fév 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
      
      
 le 6 Fév 2018
				
      Modifié(e) : Jan
      
      
 le 6 Fév 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
Voir également
Catégories
				En savoir plus sur Dates and Time dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




