版本 (14.3 KB) 作者: Jan
Convert date string to date number - C-Mex: much faster than DATENUM

3.2K 次下载

更新时间 2018/6/14


DATESTR2NUM - Fast conversion of DATESTR to DATENUM
The builtin DATENUM command is very powerful, but if the input is known to be valid and formatted exactly, a specific MEX can be much faster:
For single strings DateStr2Num is about 120 times faster than DATENUM, for a {1 x 10000} cell string, the speed up factor is 300 to 600(!), (Matlab 2011b/64, MSVC 2008).
D = DateStr2Num(S, F)
S: String or cell string in DATESTR(F) format.
In opposite to DATENUM the validity of the input string is *not* checked.
F: Integer number defining the input format. Accepted:
0: 'dd-mmm-yyyy HH:MM:SS' 01-Mar-2000 15:45:17
1: 'dd-mmm-yyyy' 01-Mar-2000
29: 'yyyy-mm-dd' 2000-03-01
30: 'yyyymmddTHHMMSS' 20000301T154517
31: 'yyyy-mm-dd HH:MM:SS' 2000-03-01 15:45:17
230: 'mm/dd/yyyyHH:MM:SS' 12/24/201515:45:17
231: 'mm/dd/yyyy HH:MM:SS' 12/24/2015 15:45:17
240: 'dd/mm/yyyyHH:MM:SS' 24/12/201515:45:17
241: 'dd/mm/yyyy HH:MM:SS' 24/12/2015 15:45:17
1000: 'dd-mmm-yyyy HH:MM:SS.FFF' 01-Mar-2000 15:45:17.123
1030: 'yyyymmddTHHMMSS.FFF' 20000301T154517.123
D: Serial date number.
C = {'2010-06-29 21:59:13', '2010-06-29 21:59:13'};
D = DateStr2Num(C, 31)
>> [734318.916122685, 734318.916122685]
Equivalent Matlab command:
D = datenum(C, 'yyyy-mm-dd HH:MM:SS')
The C-file must be compiled before using. This is done automatically at the first call of this function.
Pre-compiled Mex files can be downloaded from:

Tested: Matlab 6.5, 7.7, 7.8, 7.13, 32/64bit, WinXP/7
Compiler: LCC 2.4/3.8, BCC 5.5, Open Watcom 1.8, MSVC 2008
Compatibility to MacOS, Linux, 64 bit is assumed, but not tested.

See also: DateConvert (Jan Simon)


Jan (2023). DateStr2Num (, MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2016b
Windows macOS Linux
Help CenterMATLAB Answers 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明

Month names considered in upper and lower case.

4 new formats, automatic compilation

New format "1000": dd-mmm-yyyy HH:MM:SS.FFF
Old format "300" called "1030" now.

New integer arithmetics for >50% more speed. Support of milliseconds in yyyymmddTHHMMSS.FFF format.