Change imported date into datetime format

1 view (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 23 Aug 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 Comment
Star Strider
Star Strider on 23 Aug 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.
.

Sign in to comment.

More Answers (0)

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!