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!