0 Favourites

How do I measure the time between two numbers?

  • I am writing a small timesheet application for personal use.

    What this does is helps me keep track of exactly how many minutes I have worked, so I can bill accordingly.

    Once I've actually got it working, I plan to have it write a custom PDF for me. Easy stuff.

    Have a look at the following screencaps:

    GUI:

    Event sheet:

    Here is how it works:

    I enter a 24-hour time value (without colon) into the textbox.

    After pressing "+", the time is added to the list box. A variable called SuffixPending is set to 1, so the system knows that the next time a value is entered, it is to be appended to the same line - to make a complete timeframe row.

    It then changes the variable back to 0, so that the next line can be added.

    This all works sweet.

    BUT, I am completely stumped on how to get C2 to treat the values as base60 (I think that's the term I'm looking for - I am absolutely terrible at maths). What I'm trying to say is for example, for the first screenshot there, I want it to return "60" instead of "300" in the TotalMinuteCount variable - and therefore also in the text label down the bottom.

    Preferably, if possible, I would like to make it so that it returns total hours to two decimal places (eg, for 0230 - 0400, it should show "1.5" - instead of minutes. ButI'll work with whatever answers people can come up with.

    Please explain in simple terms. As I stated before, I am really bad at maths.

    I've already had a look through Scirra's manual about the Timer control. Their guide is useless as it only shows how to convert seconds to different minutes/hours.

    For those confused by the event sheet, the extra variables and actions there such as "ListItemCountUpTo" exist to count the rows correctly. That part is working fine.

    Thanks!

  • So the first two digits are hours and the second two are minutes. So you could convert a string like "0245" to a decimal hours with:

    Int(Left(text,2)) + int(right(text,2))/60

    Which will give 2.75

    In simpler terms it's just this:

    Hours + minutes/60

  • Thanks for the fast reply.

    If I understand correctly, what you're suggesting is to convert any value in the list as soon as it is added?

    If so, I'm not sure - I do need to make sure the list values are still readable after adding. Perhaps there's a way to convert the numbers at the time the rows are all added up, and pushed to the global variable?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Not as soon as it's added, just when you need to do the time calculation. Now you could convert it to minutes or hours just as easy. So for example you could do this:

    TotalMinutes = 0

    ListText = "0230-0420"

    time1 = tokenAt(ListText, 0, "-")

    time2 = tokenAt(ListText, 1, "-")

    minutes1 = int(left(time1, 2))*60 + int(right(time1, 2))

    minutes2 = int(left(time2, 2))*60 + int(right(time2, 2))

    TotalMinutes = TotalMinutes + minutes2-minutes1

    Set text to zeropad(int(TotalMinutes/60), 2) & TotalMinutes%60

    Which would give your result in hours and minutes or "0150".

    If instead you want it display the total minutes in hours you can do this:

    Set text to TotalMinutes/60

    which would give "1.83333333'

    Or if you always just want only two decimal places you can do this:

    Set text to int(TotalMinutes/60) & "."& zeropad(int((TotalMinutes%60)*100/60), 2)

    which would give "1.83"

  • Thank you so much for this!

    It took a while for me to read through and get through my head, but in the end I found a way to get it to work properly and add each list item.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
234 61,221
DARKSETH's avatar
DARKSETH
Unread hot topic Locked topic
198 18,656
Tom's avatar
Tom Construct Team Founder
Unread hot topic
99 10,136
karshinkoff's avatar
karshinkoff