Drop whole part of a number representing time

What would be the best way to drop whole part of a decimal number in PowerShell?

I have a number 102 which represents 102 seconds.
I want to drop minutes out of these seconds such that the result if free of minutes.

There result (calculation) should therefore be something along this:
split minutes and seconds: 102 / 60 = 1.7
drop minutes: 1.7 - 1 = 0.7
calculate seconds: 60 * 0.7 = 42

If I got this right, this means there are 42 seconds (and 1 minute which is not important right now)
Test is true: 1 * 60 + 42 = 102 // OK

I need the most efficient and conventional way to calculate this preferably by using .NET, but pure PowerShell version is fine too.

Use the modulo operator?

102%60 = 42

1 Like

wow, I use modulo so rarely that I forgot it exists!

Thanks a lot for this!

If it’s about time you could use a proper TimeSpan type …

$Time = New-TimeSpan -Seconds 102

and there’s more …


I didn’t know about New-TimeSpan, very elegant solution!