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.
0 个评论
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!