Convert different datetimes into one format

1 回表示 (過去 30 日間)
Ha Sen
Ha Sen 2020 年 4 月 23 日
コメント済み: Ha Sen 2020 年 4 月 24 日
Hi everyone,
I'm having 2 different date types (1300 rows) in one column (see below). How can I convert them into one date form. I tried different suggestions, it worked only for one type the other one showed NaT. I would prefer (xx/yy/zzzz). I appreciate your help.
'03-Sep-2009'
'17/3/2009'
'05-Feb-2009'
'05-Apr-2009'
'05-Sep-2009'
'18/5/2009'
'25/5/2009'
'28/5/2009'
'28/5/2009'
'28/5/2009'
'06-Feb-2009'
'06-Mar-2009'
'06-Apr-2009'
'06-Apr-2009'
'06-Apr-2009'
'06-Apr-2009'
'06-Apr-2009'
'06-Apr-2009'
'06-Apr-2009'

採用された回答

dpb
dpb 2020 年 4 月 24 日
Bestest would be to fix the input source to use the same format but you can try some subterfuges to see if you can sneak by without having to parse every one individually to determine which format it is on its own. datetime will do that on its own (at a high overhead so it may get slow), but you can try--with the list above as cellstr array:
>> cellfun(@(s)datetime(s,'format','dd\MM\yyyy'),dstr)
ans =
19×1 datetime array
03\09\2009
17\03\2009
05\02\2009
05\04\2009
05\09\2009
18\05\2009
25\05\2009
28\05\2009
28\05\2009
28\05\2009
06\02\2009
06\03\2009
06\04\2009
06\04\2009
06\04\2009
06\04\2009
06\04\2009
06\04\2009
06\04\2009
>>
If you get one of the ambiguous forms like 01\01\2009 you'll get a warning and will have to determine whether the default logic is correct or not...unfortunately, I don't think datetime tells you which particular entry(ies) was(were) the bad one(s)
Alternatively, do the first thing as passing the array, then locate the isNAT locations and reprocess them with the proper input format and place them where they belong. That's a loop for as many distinct input forms there are.
All in all, fixing the source might still be the easier and surely would be the better way.
  1 件のコメント
Ha Sen
Ha Sen 2020 年 4 月 24 日
Great! Thank you.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by