I want to convert one time format into another type

14 次查看(过去 30 天)
I have date and time data in this form '16-Aug-0022 04:39:55 PM' and I want to convert it in this form '08/16/2022 16:39:00'. Can you provide me with corresponding scripts
thank you in advance
  1 个评论
dpb
dpb 2022-8-20
While @Star Strider provides a way to read these, I'd ask how got the malformed date string in the beginning and suggest fixing that issue.

请先登录,再进行评论。

回答(2 个)

Chunru
Chunru 2022-8-20
编辑:Chunru 2022-8-20
str = '16-Aug-0022 04:39:55 PM';
t = datetime(str, 'InputFormat', 'dd-MMM-yyyy hh:mm:ss a'); % convert to datetime type
t.Year = t.Year + 2000; % add 2000 year
t.Format = 'MM/dd/yyyy HH:mm:ss'; % change the output format
t
t = datetime
08/16/2022 16:39:55

Star Strider
Star Strider 2022-8-20
Another approach —
dt1 = '16-Aug-0022 04:39:55 PM';
dt2 = datetime(dt1, 'InputFormat','dd-MMM-00yy hh:mm:ss a', 'Format','MM/dd/yyyy HH:mm:ss')
dt2 = datetime
08/16/2022 16:39:55
Note the specific 'InputFormat' string that accounts for the first two numbers of the year being '00'. No other conversion required!
.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by