PowerShell: How to Convert Seconds to Hours and Minutes


Often you may want to convert seconds to hours and minutes in PowerShell.

You can use the following syntax to do so:

[timespan]::fromseconds(54680).ToString()

This particular example will convert 54,680 seconds to the equivalent number of hours, minutes and seconds.

Note that we use the [timespan] command to calculate the number of days, hours, minutes, seconds, and milliseconds that are equivalent to the number of seconds that we specified.

We then use the ToString() method to convert the output to a hh:mm:ss format.

Note that we could also use the following syntax to only return the total number of hours or minutes that are equivalent to the number of seconds that we specified:

$TotalHours = [timespan]::fromseconds(54680).TotalHours
$TotalMinutes = [timespan]::fromseconds(54680).TotalMinutes

The following examples show how to use each of these methods in practice.

Example: How to Convert Seconds to Hours and Minutes in PowerShell

Suppose that we would like to convert 54,680 seconds to hours, minutes and seconds.

We can use the following syntax to do so:

[timespan]::fromseconds(54680).ToString() 

The following screenshot shows how to use this syntax in practice:

PowerShell convert seconds to hours, minutes and seconds

From the output we can see that 54,680 seconds is equivalent to 15 hours, 11 minutes and 20 seconds.

We can also use the following syntax to convert 54,680 seconds to minutes:

[timespan]::fromseconds(54680).ToMinutes

The following screenshot shows how to use this syntax in practice:

PowerShell convert seconds to minutes

From the output we can see that 54,680 seconds is equivalent to about 911.33 minutes.

Lastly, we can use the following syntax to convert 54,680 seconds to hours:

[timespan]::fromseconds(54680).ToHours

The following screenshot shows how to use this syntax in practice:

PowerShell convert seconds to hours

From the output we can see that 54,680 seconds is equivalent to about 15.19 hours.

Feel free to use whichever method you prefer.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Compare Dates
PowerShell: How to List Files in Directory by Date
PowerShell: How to Convert Unix Timestamp to Datetime
PowerShell: Get List of Files Modified After Certain Date

Leave a Reply