# how to find time difference between two times?

586 views (last 30 days)
siki on 3 Dec 2016
Commented: Star Strider on 27 Jan 2022
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?
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 3 Dec 2016
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 CommentsShowHide 1 older comment
Star Strider on 27 Jan 2022
@Shawn McCullough — My pleasure!

Sign in to comment.

### More Answers (1)

Andrei Bobrov on 3 Dec 2016
Edited: Andrei Bobrov on 3 Dec 2016
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
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!