fail to convert datetime with pm string
4 次查看(过去 30 天)
显示 更早的评论
Hi:
I successfully convert string '2022-09-30 10-01' to datetime using command:
datetime('2022-09-30 10-01','InputFormat','yyyy-MM-dd hh-mm')
but string '2022-09-30 16-01' failed, I expect it return 4:01 pm
is there any mistake with my command?
Thanks!
0 个评论
采纳的回答
Steven Lord
2022-9-30
s = '2022-09-30 16-01';
In the InputFormat option for the datetime function, 'hh' refers to "Hour, 12-hour clock notation using two digits" of the date and time string. 'HH' refers to "Hour, 24-hour clock notation using two digits". See the description of the Format property on the datetime documentation page for the table of components that you can use in the values for InputFormat and Format.
dt = datetime(s, 'InputFormat', 'yyyy-MM-dd HH-mm')
You may want to include hh and a in the Format if you want to see am/pm.
dt.Format = 'yyyy-MM-dd hh-mm a'
更多回答(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!