locked
how to represent a number (string or INT) as HH:MM:SS RRS feed

  • Question

  • I am parsing a log file and on each line it gives me milliseconds from when the job/log was started as each event occurs. Kind of like below

    230 Started doing a thing

    450 Started doing a different thing

    1150 Started doing a third thing

    What I have already done is parsed the file and captured start time and end time and done the math to determine number of milliseconds run time was between events.

    Now for my question. if I have a number that represents milliseconds, lets say 21145, how can I (easily) change that to show HH:MM:SS? I believe I need to look at the conditional formatting (like here, https://devblogs.microsoft.com/scripting/use-powershell-and-conditional-formatting-to-format-numbers/) but I don't see what I need or perhaps I don't understand it. Also, the end result doesn't have to be a date time necessarily, it could be a string or integer.

    Thank you very much!

    Thursday, July 23, 2020 2:32 PM

Answers

  • Easie-Peasie!

    $ms = 21145
    $ts = [TimeSpan]::FromMilliseconds($ms)
    "{0:d2}:{1:d2}:{2:d2}" -f $ts.Hours,$ts.Minutes,$ts.seconds



    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Robert Little Thursday, July 23, 2020 5:28 PM
    Thursday, July 23, 2020 3:01 PM

All replies

  • Easie-Peasie!

    $ms = 21145
    $ts = [TimeSpan]::FromMilliseconds($ms)
    "{0:d2}:{1:d2}:{2:d2}" -f $ts.Hours,$ts.Minutes,$ts.seconds



    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Robert Little Thursday, July 23, 2020 5:28 PM
    Thursday, July 23, 2020 3:01 PM
  • That worked perfect, thank you Rich! I'll do some reading on the DOT NET timespan

    Thursday, July 23, 2020 6:20 PM