View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.9 | 14 ratings Rate this file 27 Downloads (last 30 days) File Size: 14.1 KB File ID: #28093 Version: 1.3
image thumbnail



Jan Simon (view profile)


01 Jul 2010 (Updated )

Convert date string to date number - C-Mex: much faster than DATENUM

| Watch this File

File Information

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)

MATLAB release MATLAB 7.13 (R2011b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (22)
18 Jul 2016 Aman Sethi  
31 Aug 2015 Alain

Alain (view profile)

Jan this is truely great.

Have you reflected about datestr also? this one seems even slower - having a DateNum2Str would nicely complement...

Comment only
05 Jan 2015 Ingrid

Ingrid (view profile)

reading in large csv-files with dates in the first column turned out to be extremely slow due to the datenum function and this file save the day

15 Aug 2014 Felipe

Felipe (view profile)

02 Aug 2014 Jan Simon

Jan Simon (view profile)

@joh: What exactly is an "array of dates"? Cell strings are handles internally already.

Comment only
30 Jul 2014 joh

joh (view profile)

hi, would you use a for loop for an array of dates?

Comment only
19 Feb 2014 scott worland

Perfect! I am using 15 minute interval stream records --- my data sets are around 0.5 million lines (Elapsed time is 0.019273 seconds to convert dates to number using DateStr2Num)

25 Oct 2013 Jan Simon

Jan Simon (view profile)

@James: I do not understand the question. Of course I've created the C-file, before it could be compiled by the mex command.
I suggest to post the error you get form the compiler and explain the required details.

Comment only
10 Oct 2013 James

James (view profile)

Excellent work Jan! How did you create the c file before using the mex command? I keep getting errors from matlab compiler.


15 Apr 2013 Reza Farrahi Moghaddam  
25 Dec 2012 Hao Shen  
23 Jul 2012 Swasti Khuntia

Excellent stuff !!!!

17 Jun 2012 German Gomez-Herrero

Exactly what I was looking for! This is really good stuff

09 May 2012 Jonathan Sullivan

Very nice submission. It is enormously faster than datenum. Another great file from Jan.

24 Aug 2011 Jan Simon

Jan Simon (view profile)

To convert a serial date number to a '2011-08-24 23:38:44' date string:
sprintf('%.4d-%.2d-%.2d %.2d:%.2d:%.2d', datevecmx(now, 1));

Comment only
06 Aug 2011 Saad

Saad (view profile)

ignore my previous comment...the string had some trailing whitespace which was leading the c program to throw an exception.

06 Aug 2011 Saad

Saad (view profile)

Hi, thanks for writing this, it works great. But it seems like the C compiled version doesn't accept a char string? I have a char string of 1x25 which works with the normal .m function but I get a warning that the format is not acceptable within the compiled version. I have to use the cellstr() function to convert, before passing to the compiled version. In the end, the run time between compiled and .m function are the same. Anyway you can adapt this?

Comment only
22 Jul 2011 Todd

Todd (view profile)

My bad. Like Nate, I figured it out - what a difference! The command:
mex -O DateStr2Num.c
Generated a DateStr2Num.mexw64 file and wham! My code was at 90s using datenum. Now that section takes 0.02s. Thanks!

07 Jul 2011 Nate Jensen

Sorry, I'm retarded, I figured it out.

Comment only
07 Jul 2011 Nate Jensen

Good function, I use it all the time. I am retarded at C though. Could you tell me how to run the C function from Matlab? Thanks.

20 Apr 2011 Jan Simon

Jan Simon (view profile)

@Reyna: The new '300' format contains milliseconds also.

Comment only
21 Mar 2011 Reyna

Reyna (view profile)

This is great. Is it possible to add millisec? Particularly to extend format 30 to yyyymmddTHHMMSS.FFF?

29 Mar 2011 1.1

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

29 Jul 2013 1.2

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

30 Dec 2014 1.3

4 new formats, automatic compilation

Contact us