Datetime conversion input format

22 ビュー (過去 30 日間)
Robin Schäfer
Robin Schäfer 2020 年 6 月 23 日
コメント済み: Robin Schäfer 2020 年 6 月 23 日
Hello everyone,
i got a problem while converting strings to datetime. My data come from a table in the format e.g. '013044' which should equal 'HHMMSS'. Therefore I suggested this code:
TT.Time=datetime(TT.TimeString,'InputFormat','HHMMSS','Format','HH:MM:SS');
However, the result I get is "shifted" and i get in the upper example values like "00:13:04". I attached my data and the results.

採用された回答

Stephen23
Stephen23 2020 年 6 月 23 日
編集済み: Stephen23 2020 年 6 月 23 日
According to the datetime documentation, the correct format characters for times are:
  • hours: 'HH' or 'hh' (for 24/12 hour clocks respectively)
  • minutes: 'mm'
  • seconds: 'ss'
Using the documented time format strings does not seem to give any problems:
>> TT.Time = datetime(TT.TimeString,'InputFormat','HHmmss','Format','HH:mm:ss')
TT =
TimeString Time
__________ ________
'002039' 00:20:39
'011347' 01:13:47
'014356' 01:43:56
'002039' 00:20:39
'004039' 00:40:39
'011347' 01:13:47
'002039' 00:20:39
'002039' 00:20:39
'002148' 00:21:48
'002227' 00:22:27
'002320' 00:23:20
'002407' 00:24:07
'002548' 00:25:48
... lots of rows here
'011810' 01:18:10
'012401' 01:24:01
'012632' 01:26:32
'012751' 01:27:51
'013029' 01:30:29
'013157' 01:31:57
'013357' 01:33:57
'013533' 01:35:33
'013806' 01:38:06
'014026' 01:40:26
You should also pay attention to other relevant parts of the documentation, e.g. "If infmt does not include a date specifier, then datetime assumes that the values in DateStrings occur during the current day". A duration object may be more suitable for your data.
  1 件のコメント
Robin Schäfer
Robin Schäfer 2020 年 6 月 23 日
Thank you Stephen. I kind of expected a small mistake like this in the input format... I will check the convertion to a duration object, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by