필터 지우기
필터 지우기

how to find time difference between two times?

조회 수: 176 (최근 30일)
siki
siki 2016년 12월 3일
댓글: Star Strider 2022년 1월 27일
t1={'01-Oct-2011 11:12:00'};
t2={'01-Oct-2011 11:15:00'};
t11=datenum(t1(:));
t22=datenum(t2(:));
I have two times. How can i find difference in seconds between them?

채택된 답변

Star Strider
Star Strider 2016년 12월 3일
The etime function does exactly what you want. You have to convert your times to date vectors first:
t1={'01-Oct-2011 11:12:00'};
t2={'01-Oct-2011 11:15:00'};
t11=datevec(datenum(t1));
t22=datevec(datenum(t2));
time_interval_in_seconds = etime(t22,t11)
time_interval_in_seconds =
180.0000
  댓글 수: 2
Shawn McCullough
Shawn McCullough 2022년 1월 26일
this is great! thank you!!
it also works if you need to include milliseconds:
>> t1 = 'January 02, 2000 11:59:28.000';
>> t2 = 'January 02, 2000 17:22:46.486';
>> t1 = datevec(t1,'mmmm dd, yyyy HH:MM:SS.FFF');
>> t2 = datevec(t2,'mmmm dd, yyyy HH:MM:SS.FFF');
>> duration = etime(t2,t1)
duration =
1.939848600000000e+04
Star Strider
Star Strider 2022년 1월 27일
@Shawn McCullough — My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2016년 12월 3일
편집: Andrei Bobrov 2016년 12월 3일
t1={'01-Oct-2011 11:12:00'}; t2={'01-Oct-2011 11:15:00'};
out = seconds(diff(datetime([t1;t2])))
or
t1={'01-Oct-2011 11:12:00'}; t2={'01-Oct-2011 11:15:00'};
out = diff(datenum([t1;t2]))*24*3600

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by