Convert datetime to default format

My input date time is 16:30:00 13-05-21. It's in HH:MM:SS dd-mm-yy format. I need to convert it into default format like this (13-May-2021 16:30:00).
I tried this but, the output comes out as NaT .
inp_time='16:30:00 13-05-21';
>> dtime=datetime(inp_time,'InputFormat','HH:MM:SS dd-mm-yy')
dtime= NaT
Can you help me here?

回答 (2 件)

SALAH ALRABEEI
SALAH ALRABEEI 2021 年 6 月 7 日

0 投票

% Use 'dd-MMM-uuuu HH:mm:ss'
Steven Lord
Steven Lord 2021 年 6 月 7 日

0 投票

inp_time='16:30:00 13-05-21';
dtime=datetime(inp_time,'InputFormat','HH:MM:SS dd-mm-yy')
Warning: The format 'HH:MM:SS dd-mm-yy' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.
Warning: The format 'HH:MM:SS dd-mm-yy' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.
Error using datetime (line 647)
Unable to convert '16:30:00 13-05-21' to datetime using the format 'HH:MM:SS dd-mm-yy'.
Have you tried swapping the M and m parts of the InputFormat as suggested by the warning messages?

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2021 年 6 月 7 日

回答済み:

2021 年 6 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by