Problem in using datetime
4 次查看(过去 30 天)
显示 更早的评论
Hi, I have timestamp in the form of strings, for e.g.
e =
1×1 cell array
{'8-3-2021 9-03-51 PM'}
However, when I use the following code to comvert it into "datetime" format, I end up with this :
new1 = datetime(e, 'InputFormat', 'M-d-yyyy H-mm-ss a', 'Format', 'yyyy-M-d H-mm-ss a');
new1(isnat(new1)) = datetime(e(isnat(new1)), 'InputFormat', 'M-d-yyyy H-mm-ss a');
I also get the warning :
Warning: The format 'M-d-yyyy H-mm-ss a' contains fields for 24 hour of day (H) and for day period (a). This will cause unexpected results when converting from text. See the
datetime.Format property for a complete description of the identifiers used in datetime formats.
>> new1
new1 =
datetime
2021-8-3 12-03-51 PM
I tried different things but could not fix it.
I don't quite understand why the time changes to 12 at the hour position from the original 9.
Any help is much appreciated.
2 个评论
Sebastiano Marinelli
2021-8-6
I think the problem is in using the capital "h" try this code should work:
e = {'8-3-2021 9-03-51 PM'};
new1 = datetime(e, 'InputFormat', 'M-d-yyyy h-mm-ss a', 'Format', 'yyyy-M-d h-mm-ss a');
new1(isnat(new1)) = datetime(e(isnat(new1)), 'InputFormat', 'M-d-yyyy h-mm-ss a');
Proably using the capital H means that you use the 24h format not the 12 format. Let me know if it works
采纳的回答
Dave B
2021-8-6
The H in your format is referring to a 24-hour clock hour, as in 14 to represent 2pm, but that doesn't make sense as you're also specifying "PM". I suspect you need to change this to a lowercase h.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!