String convertion to date
Show older comments
Hello. I have the following strings
a= '16deNoviembredel2017'
b= '20:56:14'
I would like to convert the string into a specific date time format as indicated below
dt_event= datetime([a,b],'InputFormat','ddMMMMyyyyHH:mm:ss', 'Format','yyyy_MM_dd HH_mm_ss');
Could you please help me to correct the inputFormat to make it readable. Thank you.
Jorge
4 Comments
Jan
on 7 Feb 2023
"deNoviembredel" is a hard problem. Is there no chance to get a more standardized input format?
Jorge Luis Paredes Estacio
on 7 Feb 2023
Jan
on 7 Feb 2023
Then my question is: Can you ask the authors of these files to use a less obscure date format? Text files are wanted to be processable by code, so why deciding for a format which is hard to parse?
But I know: In many cases it is impossible to modifiy the inputs and the programmers have to live with strange data.
Jorge Luis Paredes Estacio
on 7 Feb 2023
Accepted Answer
More Answers (1)
Benjamin Kraus
on 7 Feb 2023
Edited: Benjamin Kraus
on 7 Feb 2023
Note, I don't speak Spanish, so I'm not sure of the proper user of "de" and "del", so this answer may need to be adjusted.
I suspect you are going to need to manually remove "de" and "del" before you can use datetime. For example:
a = '16deNoviembredel2017';
b = '20:56:14';
a_ = regexprep(a,'(\d{2})de(\w+)del(\d{4})','$1$2$3')
dt_event= datetime([a_,b],'InputFormat','ddMMMMyyyyHH:mm:ss', ...
'Format','yyyy_MM_dd HH_mm_ss','Locale','es_ES')
1 Comment
Jorge Luis Paredes Estacio
on 7 Feb 2023
Categories
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!