Subtract two 24 hour times

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

sxpapado
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10
Joined: Tue Oct 04, 2011 5:54 pm

Subtract two 24 hour times

Post by sxpapado » Mon Jul 09, 2012 2:33 am

Hi All,

Does anyone have any guidance or a script on subtracting two 24 hour time formats? For example, 02:30 minus 21:45 to come up with 4:45 or 18:46 minus 17:50 to come up with 00:56. These two main numbers would be entered into two separate fields with the result to be displayed in another field and converted into tenths in that third field. So the first result of 04:45 should be 4.8 and the later result of 00:56 should be 0.9 as per the data attached. I have been trying this with the convert function but have been unsuccessful. I do know how to convert to tenths but figured I'd add that in here as this is the end result I'm looking for. Any help is greatly appreciated. Thanks

MINUTES TENTHS
01 .0
02 .0
03 .1
04 .1
05 .1
06 .1
07 .1
08 .1
09 .2
10 .2
11 .2
12 .2
13 .2
14 .2
15 .3
16 .3
17 .3
18 .3
19 .3
20 .3
21 .4
22 .4
23 .4
24 .4
25 .4
26 .4
27 .5
28 .5
29 .5
30 .5
MINUTES TENTHS
31 .5
32 .5
33 .6
34 .6
35 .6
36 .6
37 .6
38 .6
39 .7
40 .7
41 .7
42 .7
43 .7
44 .7
45 .8
46 .8
47 .8
48 .8
49 .8
50 .8
51 .9
52 .9
53 .9
54 .9
55 .9
56 .9
57 1.0
58 1.0
59 1.0
00 .0

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Subtract two 24 hour times

Post by dunbarx » Mon Jul 09, 2012 5:05 am

Hi.

You could use your lookup table, i suppose, but should not need to.

Convert each time to seconds. Subtract the larger from the smaller, to get the absolute difference, or better, just get the "abs" of the difference. Can you do the math to convert back to minutes and seconds?

The conversion from a fraction of 60 to a fraction of 10 should not be a problem:.

Here is a test (untested).

Code: Select all

on mouseUp
   put 47/60 into temp
   set numberFormat to "0.0"
   answer temp
end mouseUp
Craig Newman

sxpapado
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10
Joined: Tue Oct 04, 2011 5:54 pm

Re: Subtract two 24 hour times

Post by sxpapado » Mon Jul 09, 2012 8:15 pm

Thanks, but will that work with the initial issue of subtracting a morning time from an evening time. i.e., 0200 minus 2100? An absolute difference here would give me 19 hours versus the correct 5 hours. Or am I missing something. Thanks for your help.....

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Subtract two 24 hour times

Post by Dixie » Mon Jul 09, 2012 8:39 pm

You are going to have to take the date into account if you want to bridge the divide between different days... So, have a look at date items and date in the dictionary.

be well

Dixie

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Subtract two 24 hour times

Post by dunbarx » Mon Jul 09, 2012 8:48 pm

Converting to seconds obviates the issue of crossing midnight. Or am I still missing something?

Craig Newman

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Subtract two 24 hour times

Post by Dixie » Mon Jul 09, 2012 9:23 pm

Craig...

From the original post...
For example, 02:30 minus 21:45 to come up with 4:45 or 18:46 minus 17:50 to come up with 00:56
Doing it this way does not make much sense to me... I agree with you that using the seconds should not matter, but it will depend on how the calculation is set up... It just seems to me that to come up with 04h45 in the first example then the 21h45 needs to be the first parameter and 02h30 the second... ( I am thinking here that time always goes forward, as it were...:-)) ... so, everything being equal then the second example should have been posted as 17h50 to 18h46 to get to the answer of 56 mins...

But maybe I was just thinking that all the time calculation would be within the one 24 hour period...

I'll shut up !...

Be well

Dixie

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Subtract two 24 hour times

Post by dunbarx » Mon Jul 09, 2012 11:27 pm

Dixie.

There is every reason to pick apart any discussion involving times. These are tricky at the very least, as you know.

Sxpapdo: Converting your values to the seconds takes into account the order of time. Those issues are "built-in" to that function, so to speak, and the earlier time will always be subtracted from the later. If the "abs" function is applied to the difference, then the order of subtraction is unimportant, which is convenient. Play with this function in a test stack until you are familiar with it.

I also assumed all is within a single 24 hour period.

Are you OK with the other stuff, formatting your results and converting to tenths without having to use a look-up table?

Craig Newman

sxpapado
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10
Joined: Tue Oct 04, 2011 5:54 pm

Re: Subtract two 24 hour times

Post by sxpapado » Tue Jul 10, 2012 9:08 pm

Thanks all for your reply's. Craig, actually this has to be accurate for calculations going into the next 24 hour day. So going from 10pm (22:00) to 02am (02:00) has to add up to 4 hrs and not 20hrs (or 18hrs depending on how it calculates). Will converting to seconds and using the abs function here work?

I set this up in excel and even Excel has an issue with how you input the Data. For example, to correctly do this in Excel crossing into the next 24 hr period, you have to enter 02:00 (2am) as 26:00 (04:00 as 28:00 etc.). Once you Tab out of the cell, it will display 02:00 and do the math correctly but it's a weird way of data entry for crossing into the next day and not intuitive. I was hoping Live code could be of help here.

I'm new to coding so if you could provide with an example would greatly be appreciated. I'm reading up on these items in the discussion as we speak :-) Thanks again.

Regards

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Subtract two 24 hour times

Post by mwieder » Tue Jul 10, 2012 9:29 pm

If you really don't want to store the date along with the time (I can't imagine why not, but I suppose you have your reasons) then

(if you know which is the starting time and which is the ending time)

Code: Select all

if endingTime < startingTime then
  add 24 hours to endingTime
end if
convert both to seconds
subtract startingTime from endingTime

voila

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Subtract two 24 hour times

Post by dunbarx » Wed Jul 11, 2012 1:24 am

Hi again.

Please do some experimenting. It is the only way to learn this stuff.

By converting your times to "the seconds" you obviate the issue of what day or year you start and end those times, or what format your time is displayed in.

"The seconds" is a system counter (one of several similar types) that simply runs from an arbitrary starting point. So if you convert one time, for example, you might get a value of 1000. If you convert a later time, you might get, say, 2000. The difference is 1000 seconds. Period. It is quite irrelevant what, where and how you get those times.

Do you see how this cuts through all the formatted time issues, midnights, lunar eclipses, etc.? An absolute counter has no such entanglements. When you get your 1000 seconds, you can convert this back to hours, minutes, or quatloos, whatever,

So play with this already. The task you will ultimately have will be all about forming your output to display the way you want it

Craig Newman

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Subtract two 24 hour times

Post by mwieder » Wed Jul 11, 2012 1:59 am

Craig-

Yes, but... if the time is specified in just hours:minutes then converting to seconds still doesn't do the trick. There's no way to tell the difference between 11PM yesterday and 11PM tomorrow or be able to tell what day 8AM refers to.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Subtract two 24 hour times

Post by dunbarx » Wed Jul 11, 2012 3:14 am

Mark.

Rereading from the beginning, I see your point. Yours too, Dixie.

The system will take an "incomplete" time like "22:00" and "01:00" and assume that both lie within the same day.

sxPapado. Your script must append, or somewhere behind the scenes include, the day in which the two times occur. i do not see a way for LC, or anything, to know which way you intend to make your interval without this information.

Or is it possible to do the subtraction both ways, and simply select the smaller value? This seems dicey.

As usual, the problem is larger than first it appears. I am frequently guilty of falling into this. But there is always a way around these things. Where shall we go from here? Can you know the date as well as the time? Or does the smaller difference suffice?

Craig Newman

sxpapado
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10
Joined: Tue Oct 04, 2011 5:54 pm

Re: Subtract two 24 hour times

Post by sxpapado » Wed Jul 11, 2012 2:54 pm

Mark and All, the start date would be included . I guess I should have given you folks exactly what I'm trying to accomplish. I also thought this would be simple. But from working with Excel and now this, it may be more complex for a simple task.

Form entry outline left to right:

Date Start work Stop Work Total time in hours and tenths

07/11/2012 18:00 02:30 8.5

Some of the work is clocked out at some time during the hour. Like 02:43 so the tenths table I included has to be followed. Notice that 1.0 hours exists from 00:57 to 00:02 and .1 exists from 00:03 to 00:08, etc.
Some programs I have come across have used automated calculations using 1 tenth (0.1) going from 00:01 to 00:06. I need to use my table.

Thanks for all the help.

LittleGreyMan
Posts: 49
Joined: Sat Jun 16, 2012 7:57 pm

Re: Subtract two 24 hour times

Post by LittleGreyMan » Wed Jul 11, 2012 6:21 pm

Hi,

If I understand well, your input data is:

-a start date
-a start hour
-a stop hour

You want to return the difference in decimal hour (rounded to the tenth) between the stop hour and the start hour.

One data is missing: the stop date, or the maximum duration.

If it is supposed to be less than 24 hours, just use mwieder (Mark, I guess?) pseudo-code, it will do the trick.

Once you have the result in seconds, divide it by 3600 and round it to 1/10 to get decimal hours. No table needed.

No rocket science, but the problem has to be clearly and completely defined in order to be solved. That's 80% of the work.

HTH
Best regards,

Didier

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Subtract two 24 hour times

Post by mwieder » Wed Jul 11, 2012 7:03 pm

Once you have the result in seconds, divide it by 3600 and round it to 1/10 to get decimal hours. No table needed.
..thanks... I knew there was a way to do this without using a table.

Post Reply