datetime without leading zero

datetime('212704','InputFormat','Hmmss')
gives an error.
datetime('21:27:04','InputFormat','H:mm:ss')
does not.
This is a problem when importing time noted as hours without leading zero as in the first format using textscan. Using:
datetime('92704','InputFormat','HHmmss')
gives an error due to the lack of the leading zero. Any workaround for this?

回答 (2 件)

the cyclist
the cyclist 2017 年 11 月 25 日
編集済み: the cyclist 2017 年 11 月 25 日

1 投票

Can you sneak a formatting step between the import and the date function?
sprintf('%06.0f',92704)
ans =
'092704'
and
sprintf('%06.0f',212704)
ans =
'212704'
and then use the 2-digit hour format?

1 件のコメント

Andy
Andy 2017 年 11 月 25 日
The application is a constant feed from a server read as textstrings in a given format. Ex:
textscan(msg,'%u %u %s %{Hmmss}D');
I guess best workaround as suggested is to import time as an integer and then do the format of the time field according to 5 or 6 digits. Just kinda weird and a bit annoying that Matlab can't handle this time notation by itself.

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

Greg
Greg 2017 年 11 月 25 日
編集済み: Greg 2017 年 11 月 25 日

1 投票

datetime(datenum('92704','HHMMSS'),'ConvertFrom','datenum')
Seems to work. Same concept as the cyclist's answer, except this method is likely easier to vectorize.

カテゴリ

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

質問済み:

2017 年 11 月 25 日

コメント済み:

2017 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by