Converting milliseconds to minutes

This topic contains 13 replies, has 3 voices, and was last updated by Profile photo of Venkata Kalyan Venkata Kalyan 4 months, 3 weeks ago.

  • Author
    Posts
  • #47085
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi,
    I have log file which contains the below information:
    Data started: 02:39
    Data ended: 04:25
    Time taken: 60000

    Data started: 04:39
    Data ended: 02:25
    Time taken: 70000

    I want "Time Taken" value to convert into minutes.

    Can someone please help me on this?

    -Kalyan

  • #47087
    Profile photo of Don Jones
    Don Jones
    Keymaster

    What is the value now? Seconds?

  • #47089
    Profile photo of Craig Duff
    Craig Duff
    Participant
    New-TimeSpan -Seconds 70000
    New-Timespan -Seconds (70000/1000)
    [Math]::Round(70000/60)
    [Math]::Round(70000/(1000*60))

    You can use New-Timespan or just a math equation.

    • This reply was modified 4 months, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
    • This reply was modified 4 months, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
    • This reply was modified 4 months, 3 weeks ago by Profile photo of Craig Duff Craig Duff.
  • #47094
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi Jones,
    Now, it is milliseconds.

    -Kalyan

  • #47096
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi Craig,
    Thanks. But in the log file, every third line "Time taken" filed is there.

    How do I convert every "Time Taken" field.

    NOTE: The "Time taken" value keeps on changing.

    -Kalyan

    • #47099
      Profile photo of Craig Duff
      Craig Duff
      Participant

      So you are trying to rewrite the log file with that line converted?

  • #47101
    Profile photo of Don Jones
    Don Jones
    Keymaster

    It's actually "Don" or "Mr. Jones" 🙂

    So if the start is...

    Data started: 02:39
    Data ended: 04:25
    Time taken: 60000
    
    Data started: 04:39
    Data ended: 02:25
    Time taken: 70000
    foreach ($line in (Get-Content filename.txt)) {
     if ($line -like '*time*') {
        $pieces = $line -split ' '
        $milliseconds = $pieces[1]
        $seconds = $milliseconds / 1000
     }
    }
    

    and then you can do whatever you want with $seconds.

  • #47103
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi Craig,
    Or we can output the result to a new file.
    Same file will also good.

    -Kalyan

  • #47105
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi Don,
    Tried the below:
    foreach ($line in (Get-Content VIS_PRD_Data_Aggregation_Log.txt)) {
    if ($line -like 'Time taken for whole process') {
    $pieces = $line -split ' '
    $milliseconds = $pieces[1]
    $seconds = $milliseconds / 1000
    }
    }

    Nothing is happening.

    -Kalyan

  • #47107
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi,
    It says:
    Cannot convert value "taken" to type "System.Int32". Error: "Input string was not in a correct
    format."
    At C:\Users\admin\Desktop\Test\cool\test.ps1:5 char:6
    + $seconds = $milliseconds / 1000
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

    -Kalyan

  • #47111
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Apologies.

    foreach ($line in (Get-Content filename.txt)) {
     if ($line -like '*time*') {
        $pieces = $line -split ' '
        $milliseconds = $pieces[2]
        $seconds = $milliseconds / 1000
        # for example... "Time Taken: $seconds" | Out-File whatever.txt -Append
     } else {
      # for example... $line | Out-File whatever.txt -Append
     }
    }
    

    However, this isn't intended to be a complete solution for you, as I don't know what your end goal is. My intent was to help you pull out the milliseconds value and convert it to seconds. That value is in $seconds. You will have to do additional coding to achieve your final desired result. Our goal here is to help you, but speaking for myself I'm not able to take the time and analyze your needs and produce a completed script free of charge. Sorry about that miscommunication. I've added two comments to the above that can perhaps show how I would use $seconds to write out to a new log file.

  • #47115
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Thanks. Let me try on this.

    -Kalyan

  • #47123
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Modified the script & made it working: Below is the script:
    foreach ($line in (Get-Content test.txt)) {
    if ($line -like '*process*') {
    $pieces = $line -split ' '
    $seconds = [math]::round( ($pieces[6]) / 1024)
    write-host $seconds
    }
    }

    Just have a question, by using the script I am able to convert milliseconds to minutes.

    Can I replace these values in the log:
    Data started: 02:39
    Data ended: 04:25
    Time taken: 60000

    Data started: 04:39
    Data ended: 02:25
    Time taken: 70000

    -Kalyan

  • #47155
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi,
    Finally modified the script a bit & able to convert to minutes and replace in the same file.

    Thanks,
    -Kalyan

You must be logged in to reply to this topic.