Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Topic: Comparison of time entries
Replies: 11   Last Post: Apr 25, 2012 1:33 PM

 Messages: [ Previous | Next ]
 Steven Lord Posts: 17,627 Registered: 12/7/04
Re: Comparison of time entries
Posted: Apr 24, 2012 10:04 AM

"Johannes " <j.luthe@gmx.de> wrote in message
news:jn6b2f\$jc8\$1@newscl01ah.mathworks.com...
> Hi, I really hope you can help me.
>
> I'm looking for a way to compare two time entries in an Excel-File
> effectively.
> The entries are given in the form 'hh:mm:ss' e.g. '14:28:30'.
>
> I would like to figure out the chronological order of the two entries.
> So, how is it possible to determine, that '17:50:23' is later than
> '17:45:33' for example?

I'm going to assume all the times fall in the same day for purposes of this
demonstration. [The technique generalizes, but I'll leave that
generalization up to you.] I'm also going to assume that hh is between 0 and
23, mm is between 0 and 59, and ss is between 0 and 59. This avoids the
scenario where '08:60:00' is exactly the same as '09:00:00'.

If the hour sections of the two times are not equal, one of the hours must
represent a smaller number than the other. That's the earlier time.

If the hour sections of the two times are equal, consider the minutes
sections. If they're not equal, one is smaller; that's the earlier time.

If the minutes sections are equal, consider the seconds. Whichever one is
smaller is the earlier time.

> I tried to use the function datenum.
>
> a =datenum('14:52:49') = 7.364215839004630e+005
> b =datenum('15:50:49') = 7.363616255671296e+005

You should not call DATENUM or DATESTR or DATEVEC with just one input. If
you know the format of the date, call the date function with that format
string or number.

a = datenum('14:52:49', 'HH:MM:SS')
b = datenum('15:50:49', 'HH:MM:SS')
a - b

> a - b = -59.958333333372138
>
> Why is the result negative?

a - b < 0 means that a < b and so a is the earlier time.

--
Steve Lord
slord@mathworks.com
http://www.mathworks.com

Date Subject Author
4/24/12 Johannes
4/24/12 dpb
4/24/12 Steven Lord
4/25/12 Johannes
4/25/12 dpb
4/25/12 dpb
4/25/12 dpb
4/25/12 Barry Williams
4/25/12 Johannes
4/25/12 dpb
4/25/12 Barry Williams
4/25/12 Steven Lord