フィルターのクリア

Convert character string to datetime variable

2 ビュー (過去 30 日間)
Ahmad Gad
Ahmad Gad 2020 年 12 月 17 日
コメント済み: Ahmad Gad 2020 年 12 月 21 日
Hello all,
I have a character string that represents the day and time. It takes the following format:
T = '352:20:38:58.092000'
Where 355 represents the day number in the year (17-Dec-2020). Assuming that I know the year, what is the shortest way to convert this character string into a datetime variable to be
T = 17-Dec-2020 20:38:58.092000
I can do it but with multiple steps that include splitting the string to get the number of the day (352) and convert it to 'dd-mm-yyyy' format using:
Year = 2020; Day = 352;
datetime(Year,1,0) + caldays(Day);
But I am looking for the fastest way to do this, if anyone can help.
Thanks in advance!
  1 件のコメント
Ahmad Gad
Ahmad Gad 2020 年 12 月 21 日
Thanks a lot Cris for the answer. I was looking for something similar.

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

採用された回答

Cris LaPierre
Cris LaPierre 2020 年 12 月 18 日
編集済み: Cris LaPierre 2020 年 12 月 18 日
You are assuming doing this in a single step is faster. That is not always true. Just remember there may be a lot happening under the hood to compensate for compact code. Also, compact code can make it harder for others to understand your code.
Recognizing that your day and time is not a datetime but a duration (elapsed time from the beginning of the year), I would probably take the following approach.
T = '352:20:38:58.092000';
% convert to a duration
dur = duration(T,"InputFormat","dd:hh:mm:ss.SSSSSS")
dur = duration
8468:38:58
% Add the duration to Jan 0, 2020 to get datetime
d = datetime(2020,01,0)+dur
d = datetime
17-Dec-2020 20:38:58
d.Format = "dd-MMM-yyyy HH:mm:ss.SSSSSS"
d = datetime
17-Dec-2020 20:38:58.092000

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by