Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting time 121.419419 to readable minutes/seconds

I'd like to calculate the time my script runs, but my result from get-date is in totalseconds.

How can I convert this to 31:14:12 behing hours:minutes:seconds?

like image 218
Sune Avatar asked Jan 05 '12 17:01

Sune


2 Answers

PS> $ts = New-TimeSpan -Seconds 1234567
PS> '{0:00}:{1:00}:{2:00}' -f $ts.Hours,$ts.Minutes,$ts.Seconds
06:56:07

or

PS> "$ts" -replace '^\d+?\.'
06:56:07
like image 126
Shay Levy Avatar answered Oct 12 '22 15:10

Shay Levy


All you have to do is use the Measure-Command cmdlet to get the time:

PS > measure-command { sleep 5}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 5
Milliseconds      : 13
Ticks             : 50137481
TotalDays         : 5.80294918981481E-05
TotalHours        : 0.00139270780555556
TotalMinutes      : 0.0835624683333333
TotalSeconds      : 5.0137481
TotalMilliseconds : 5013.7481

The above output itself might be good enough for you, or you can format it appropriately as the the output of Measure-Command is a TimeSpan object. Or you can use ToString:

PS > (measure-command { sleep 125}).tostring()
00:02:05.0017446
like image 21
manojlds Avatar answered Oct 12 '22 15:10

manojlds