Converting milliseconds to a readable time

Share your advanced knowledge/code with the community.
Quin
Posts: 24
Joined: Wed Nov 08, 2023 4:38 pm

Converting milliseconds to a readable time

Post by Quin »

I needed the ability to turn an integer, like 4937463 into 1 hour, 22 minutes, 17 seconds. As such, I wrote this tiny procedure to do it. Hopefully it'll be useful to somebody. :)

Code: Select all

Procedure$ MSToString(MS)
  If MS < 1000
    ProcedureReturn "Less than a second"
  EndIf
  Protected Days, Hours, Minutes, Seconds, CurVal
  CurVal = MS / 1000
  Seconds = Mod(CurVal, 60)
  CurVal / 60
  Minutes = Mod(CurVal, 60)
  CurVal / 60
  Hours = Mod(CurVal, 24)
  CurVal / 24
  Days = CurVal
  Protected Res$
  If Days > 0
    !v_res$ += v_days + " " + (v_days == 1 ? "day" : "days") + ", ";
  EndIf
  If Hours > 0
    !v_res$ += v_hours + " " + (v_hours == 1 ? "hour" : "hours") + ", ";
  EndIf
  If Minutes > 0
    !v_res$ += v_minutes + " " + (v_minutes == 1 ? "minute" : "minutes") + ", ";
  EndIf
  If Seconds > 0
    !v_res$ += v_seconds + " " + (v_seconds == 1 ? "second" : "seconds");
  EndIf
  ProcedureReturn Res$
EndProcedure
Cheers!