Azzera filtri
Azzera filtri

Change imported date into datetime format

2 visualizzazioni (ultimi 30 giorni)
Hi,
I have a cell array with many dates that were imported from an excel file.
Each cell array entry looks like this ie. 08/21/2020 (Aug).
I want to change the above format into a datetime format.

Risposta accettata

Star Strider
Star Strider il 23 Ago 2020
Try this:
de = '08/21/2020 (Aug)';
DT = datetime(de, 'InputFormat','MM/dd/yyyy (MMM)')
producing (using the default Format):
DT =
datetime
21-Aug-2020
.
  1 Commento
Star Strider
Star Strider il 23 Ago 2020
That Format only works with datetime (introduced in R2014b with significant updates in R2016b), that you apparently do not have access to.
To do the same with datenum and datestr, try this:
de = ['08/21/2020 (Aug)'; '09/21/2020 (Sep)']
DT = datenum(de, 'mm/dd/yyyy')
DS = datestr(DT)
producing:
DT =
738024
738055
DS =
2×11 char array
'21-Aug-2020'
'21-Sep-2020'
So, just ignore the short month designation string in parentheses.
I created a duplicate for September to be certain it would work with more than one value in the column vector. It appears to work well with them.
.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by