Time difference between two sets of times in hrs mins and seconds.. GUI if possible

6 views (last 30 days)
Hi im new to MATLAB and trying to setup a GUI to calculate the time difference between two sets of times. Eg: 10:00:00 and 11:01:00
Difference would be 1 hr 1min and 0 seconds..
thanks in advance

Accepted Answer

Thomas
Thomas on 19 Oct 2011
Hi Sharon, I have written a code for this a long time ago. Hope this helps..
close all
% Prompt to enter the time using GUI
prompt ={'Enter Start time and End time:'};
dlg_title = 'Time difference calculator';
num_lines=1;
def={'0 0 0 1 1 1'};
str1 = inputdlg(prompt,dlg_title,num_lines,def);
time1=str2num(str1{1});
% get the times
time1hr=time1(1);
time1min=time1(2);
time1sec=time1(3);
time2hr=time1(4);
time2min=time1(5);
time2sec=time1(6);
time1out=(time1hr*60*60)+(time1min*60)+time1sec;
time2out=(time2hr*60*60)+(time2min*60)+time2sec;
% actual calculation
diffInSec=time2out-time1out
diffInMin=diffInSec/60
if diffInMin >60
Hrs=(diffInMin/60)-mod(diffInMin/60,1)
minutes=diffInMin-(Hrs*60)-mod(diffInMin,1)
seconds=diffInSec-(Hrs*60*60)-(minutes*60)
else
afterdecimal=mod(diffInMin,1);
minutes=diffInMin-afterdecimal
seconds=diffInSec-(minutes*60)
end

More Answers (2)

Sean de Wolski
Sean de Wolski on 19 Oct 2011
So start by opening GUIDE
guide %at the command line
Then draw the GUI so that it has edit boxes to enter times, static boxes to give directions, and a push button to press to give you the times. After that, write the functions to do the work. Or write the functions to do the work first and then add them to a GUI. Ask specific detailed MATLAB questions here when you get stuck.
Welcome to MATLAB Answers!

Real User
Real User on 20 Jan 2023
>> duration("11:01:00")-duration("10:00:00")
ans =
duration
01:01:00
>> string(duration("11:01:00")-duration("10:00:00"))
ans =
"01:01:00"
>> [h, m, s]=hms(duration("11:01:00")-duration("10:00:00"))
h =
1
m =
1
s =
0
>> d = duration(h,m,s)
d =
duration
01:01:00
These work even if you include the dates.
[m,d,t] = split(T,{'months','days','time'})

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!