MATLAB: How to convert a string with fractional seconds into a datetime object

datetimeformatfractional;identifiers;MATLABmillisecondsnat;parseseconds

I have a string containing date and time in the following format:
 
date_string =
"1991-03-11 08:45:15.3"
I am not able to figure out how to specify the format. I have tried 'yyyy-MM-dd  HH:mm:SS' and 'yyyy-MM-dd  HH:mm:SSS' and various other combinations, but I keep getting this error:
  ERROR: Error using datetime (line 616) Unable to parse '1991-03-11 08:45:15.3' as a date/time using the format 'yyyy-MM-dd HH:mm:SS'.
How can I convert this string into a "datetime" object?

Best Answer

As per documentation, "datetime" format notation uses "s" for whole seconds, and "S" for fractional seconds. So to convert your dates to "datetime" you can run the following command:
 
>> mydate = datetime(date_string, 'InputFormat', 'yyyy-MM-dd HH:mm:ss.S')
mydate =
datetime
11-Mar-1991 08:45:15