How do I convert a string to a datetime?

18 views (last 30 days)
Currently I have the string:
"19:00:06 01.04.20"
I want to convert this to a date, and I use the following function:
t=datetime(my_string,'InputFormat','HH:mm:ss dd.MM.yy')
but i get Error
I would be like to help :)
  1 Comment
Steven Lord
Steven Lord on 21 Nov 2020
What is the full and exact text (everything written in red in the Command Window) you receive when you try to define that string and call datetime on it? The text of the message may give crucial clues as to why MATLAB cannot successfully run that call to datetime or at least suggest the next step to take to investigate the problem.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 18 Nov 2020
Assign your string to the variable my_string
my_string = "19:00:06 01.04.20";
t=datetime(my_string,'InputFormat','HH:mm:ss dd.MM.yy')
t = datetime
01-Apr-2020 19:00:06
Cris LaPierre
Cris LaPierre on 21 Nov 2020
You can use readtable to directly import this column as a datetime. See this page.

Sign in to comment.

More Answers (0)


Find more on Dates and Time in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!