String convertion to date

5 ビュー (過去 30 日間)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 2 月 7 日
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 件のコメント
Jan
Jan 2023 年 2 月 7 日
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
Jorge Luis Paredes Estacio 2023 年 2 月 7 日
Unfortunately, you can find that information like that from acceleration records and need to modify the scripts to verify the date before processing them.

サインインしてコメントする。

採用された回答

Jan
Jan 2023 年 2 月 7 日
a = '16deNoviembredel2017';
b = '20:56:14';
dt_event = datetime([a,b], 'Locale', 'es_ES', ...
'InputFormat', 'dd''de''MMMM''del''yyyyHH:mm:ss', ...
'Format','yyyy_MM_dd HH_mm_ss')
dt_event = datetime
2017_11_16 20_56_14
This works for this specific input, but are the keys "de" and "del" really fixed? I do not speak Spanish.
  1 件のコメント
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 2 月 7 日
Thank you very much. It works perfectly.
Best
Jorge

サインインしてコメントする。

その他の回答 (1 件)

Benjamin Kraus
Benjamin Kraus 2023 年 2 月 7 日
編集済み: Benjamin Kraus 2023 年 2 月 7 日
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')
a_ = '16Noviembre2017'
dt_event= datetime([a_,b],'InputFormat','ddMMMMyyyyHH:mm:ss', ...
'Format','yyyy_MM_dd HH_mm_ss','Locale','es_ES')
dt_event = datetime
2017_11_16 20_56_14
  1 件のコメント
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 2 月 7 日
Thank you very much. It works perfectly.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by