How do I convert tall array duration time vector to HH:mm:ss for merging with tall array datetime vector ?

14 ビュー (過去 30 日間)
Douglas Leaffer
Douglas Leaffer 2022 年 1 月 21 日
コメント済み: Douglas Leaffer 2022 年 4 月 20 日
Need some help with this. I have a tall array date vector 'yyyy-MM-dd' of datetime format, and a tall array time vector 'HH:mm:ss.SSS' of duration format. How do I convert the duration vector to only HH:mm:ss (omitting the decimals) and then combine that with the date vector to get: 'yyyy-MM-dd HH:mm:ss' ?

採用された回答

Chris
Chris 2022 年 1 月 21 日
dates = tall(repmat(datetime(date),3,1))
dates = 3×1 tall datetime array 21-Jan-2022 21-Jan-2022 21-Jan-2022
times = tall(repmat(duration(15,12,1,345,'Format','hh:mm:ss.SSS'),3,1))
times = 3×1 tall duration array 15:12:01.345 15:12:01.345 15:12:01.345
times.Format = 'hh:mm:ss';
datetime([string(dates) + " " + string(times)])
ans = 3×1 tall datetime array 21-Jan-2022 15:12:01 21-Jan-2022 15:12:01 21-Jan-2022 15:12:01
  9 件のコメント
Douglas Leaffer
Douglas Leaffer 2022 年 4 月 20 日
Chris, that works fine. Thank you for your suggestions.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 1 月 21 日
perhaps
[h,m,s] = hms(DURATION_COLUMN);
DATETIME_COLUMN = TIME_COLUMN + duration(h,m,floor(s))
or perhaps
DATETIME_COLUMN = dateshift(TIME_COLUMN + DURATION_COLUMN, 'start', 'second')
if you do not have negative durations or negative datetimes, then adding first and then getting rid of the fractions of a second should give the same result.

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by