Need help subtracting and doing if statements for time
3 ビュー (過去 30 日間)
古いコメントを表示
I would like to subtract two different times in the HH:MM format from an excel sheet and to then use result to use in a if statement where I will compare the resultant time from the substraction to another period of time, lets say 30 minutes.
0 件のコメント
採用された回答
Steven Lord
2024 年 3 月 15 日
There doesn't seem to be any date information included with your data, so I'd use duration instead of datetime. I'm mostly using your data but I also wanted to show how I'd handle midnight crossings.
du = duration(["6:30", "7:30"; "8:30", "9:30"; "9:00", "10:00"; "17:45", "1:00"], ...
'InputFormat', 'hh:mm')
difference = du(:, 2)-du(:, 1)
In this case the last difference is negative, which suggests that the flight was in the air at midnight. So let's add 24 hours to that entry.
inAirAtMidnight = difference < hours(0)
difference(inAirAtMidnight) = difference(inAirAtMidnight) + hours(24)
Note that I was able to use < between the duration array difference and the duration array returned by hours(0) to perform some comparisons.
Alternately we could have modified du directly. I made a copy of du as du2 in case you wanted to run the code and experiment yourself.
du2 = du;
departure = du2(:, 1);
arrival = du2(:, 2);
arrivedBeforeDeparture = arrival < departure
du2(arrivedBeforeDeparture, 2) = du2(arrivedBeforeDeparture, 2) + hours(24)
difference2 = diff(du2, [], 2)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!