0 Favourites

Convert milliseconds to...

  • I have a timer set up which displays minutes:seconds:milliseconds. Each kind is its own variable. It's displaying it as a text box, and so I can not use it to compare best times, etc.

    I have a formula which converts all the variables into milliseconds, but am wondering if there's a way to split them back up into minutes, seconds and milliseconds.

    Or if there's a simple way to provide times and best times. It's turning into a real mind-bender.

  • If you have the time in milliseconds:

    t: time in milliseconds

    milliseconds: t % 1000

    seconds: int(t / 1000) % 60

    minutes: int(t / 60000) % 60

    So your display would be:

    (int(t / 60000) % 60) & ":" & (int(t / 1000) % 60) & ":" & (t % 1000)

  • Hi R0j0hound,

    Thank you for your reply. I'm not able to get that working I'm afraid. I currently have the code:

    Add dt to Timer

    Set minutes to floor(timer/60)

    Set seconds to floor(timer)%60

    Set milliseconds to timer%1000

    Set TimeScore text to:

    right("00"&minutes,2)&" : "&right("00"&seconds,2)&" : "&left(right("000"&float(milliseconds),3),2)

    I then convert these to milliseconds using:

    CurrentTimeVar = Milliseconds+(seconds/1000)+(Minutes/60000)

    However, this displays the milliseconds as 0.0000 and 1 second shows at 1.0000.

    The code you have given me shows the timer very strangely. When it reaches 61 seconds, the other sections of the timer does not change, and it happily counts up to over 100 without the other seconds being added:

    E.G: 0:0:101.56789

    At the moment it's being used for a best time, so I'm having to store the min, sec, and mil data. It would be a lot better if I just saved the mil and converted it to 00:00:00.

    Kind regards.

  • R0J0hound's example has the time t in milliseconds. Your timer is in seconds so the milliseconds would be:

    floor(timer * 1000) % 1000

    minutesSecondsMilliseconds.capx (r99)

  • :( That's one of the problems with beta... I can't open the example files from stable.

    Thanks for your help! I've tried to recreate what you've described, but it doesn't seem to be working, so I've installed r99 on my laptop.

    Never heard of zeropad, but this is giving exactly the same result that I currently have but with 3 milliseconds being shown rather than just 2. Judging from this, if I wanted to store the time as a best time, I would need to save all 3 things like I'm doing now (Mins/Secs/Mills)...

    Say for example I just have a random number of milliseconds. 12345678. Is there a formula which will allow me to convert this into minutes, seconds and milliseconds. This way, I only need to save the milliseconds for each level.

    My level works perfectly now, but it's untidy and I forsee it being hard to manage. It's around 24 events long, but I think I can get it lower than 10 with this other method.

  • Say for example I just have a random number of milliseconds. 12345678. Is there a formula which will allow me to convert this into minutes, seconds and milliseconds. This way, I only need to save the milliseconds for each level.

    If you have the time in milliseconds:

    t: time in milliseconds

    milliseconds: t % 1000

    seconds: int(t / 1000) % 60

    minutes: int(t / 60000) % 60

  • Hi R0j0hound,

    Thank you for your reply. I'm not able to get that working I'm afraid.

    The code you have given me shows the timer very strangely. When it reaches 61 seconds, the other sections of the timer does not change, and it happily counts up to over 100 without the other seconds being added:

    E.G: 0:0:101.56789

    Do you know what I'm missing? At the moment I have:

    Add dt to timer

    Set t to timer%1000

    Set text to (int(t / 60000) % 60) & ":" & (int(t / 1000) % 60) & ":" & (t % 1000)

    This gives me the strange series of numbers.

  • Milliseconds is seconds*1000, so your timer that counts seconds needs to be multiplied by 1000, not modulo 1000.

    Set t to timer*1000 (this)

    Set t to timer%1000 (not this)

  • Ahh, that seems to be a lot closer. I'm trying to have it display as 00:00:00, and have tweaked the code enough to show the first two sections correctly, but I'm falling down at the millisecond section which is either showing the full range of numbers, which is daft looking, or the right length but not showing the first 3 digits of the milliseconds:

    right("00"&(int(milliseconds / 60000) % 60),2) & ":" & right("00"&(int(milliseconds / 1000) % 60),2) & ":" & left(right("000"&(milliseconds % 1000),3),3)

  • Did you check out ramones' example? To me it sounds like it does everything you want except store the total time elapsed in milliseconds, is that correct?

    If it is correct, just make another variable "t" like you have in your case, and instead of the "Set text to timer*1000" action in ramones' example, do "Set t to timer*1000". Now the "t" variable will hold the total amount of time elapsed in milliseconds, and the text will show the time in "min:sec:mill" with 2, 2 and 3 numbers respectively.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Probably not the most elegant, but I've fixed with by adding a second event.

    1) right("00"&(int(milliseconds / 60000) % 60),2) & ":" & right("00"&(int(milliseconds / 1000) % 60),2) & ":" & (milliseconds % 1000)

    2) left(CurrentTime.text,9)

    This allows the code to be shown correctly, but I'm sure there's an easier way from within point 1).

    Any ideas?

  • What you really want is to display centiseconds and not milliseconds?

    minutesSecondsCentiseconds.capx (r95)

  • Fascinating... this method does it with only the use of 1 variable and 1 additional line. I can then have another variable that I can use to store the data. I really like that there's so many ways to do one thing. I like having options.

    Do you happen to know a way to have it display 00:00:000 (note the 3 at the end). This would enable me to have it as more precise.

    Thanks for your help!

    EDIT: Ahh, done it!

    zeropad(floor(timer/60),2) & ":" & zeropad(floor(timer)%60,2) & ":" & zeropad(floor(timer*1000)%1000,3)

  • "I have a timer set up which displays minutes:seconds:milliseconds. Each kind is its own variable. It's displaying it as a text box, and so I can not use it to compare best times, etc."

    AnD4D, have you managed to do this, are you able to save the best times, i am trying to do the same, but no result. Please send me capx. or a screenshot with the events, for more then 2 months i can`t finish my game because of this...

  • legora

    Yep, thanks to these guys I managed it. In the end I had the time count everything in miliseconds, and then convert this to M:S:MS.

    If I remember, I'l post a cap when I get back from work. Feel free to poke me...

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Topic
19 1,259
paradine's avatar
paradine
Unread hot topic
38 2,325
99Instances2Go's avatar
99Instances2Go
Topic
0 Favourites
convert html5 to ios
21 3,111
AnthonyB28's avatar
AnthonyB28