Convert different datetimes into one format

2 Ansichten (letzte 30 Tage)
Ha Sen
Ha Sen am 23 Apr. 2020
Kommentiert: Ha Sen am 24 Apr. 2020
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'

Akzeptierte Antwort

dpb
dpb am 24 Apr. 2020
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.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by