Change imported date into datetime format

1 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2020-8-23
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 个评论
Star Strider
Star Strider 2020-8-23
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.
.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by