Skip to content

Format-TimeSpan

SYNOPSIS

Formats a TimeSpan object into a human-readable string.

SYNTAX

Format-TimeSpan [-TimeSpan] <TimeSpan> [[-Precision] <Int32>] [[-BaseUnit] <String>] [-FullNames]
 [-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION

This function converts a TimeSpan object into a formatted string based on a chosen unit or precision. It allows specifying a base unit, the number of precision levels, and whether to use full unit names. If the TimeSpan is negative, it is prefixed with a minus sign.

EXAMPLES

EXAMPLE 1

New-TimeSpan -Minutes 90 | Format-TimeSpan

Output:

2h

Formats the given TimeSpan into a human-readable format using the most significant unit.

EXAMPLE 2

[TimeSpan]::FromSeconds(3661) | Format-TimeSpan -Precision 2 -FullNames

Output:

1 hour 1 minute

Returns the TimeSpan formatted into multiple components based on the specified precision.

EXAMPLE 3

[TimeSpan]::FromMilliseconds(500) | Format-TimeSpan -Precision 2 -FullNames

Output:

500 milliseconds 0 microseconds

Forces the output to be formatted in milliseconds, regardless of precision.

PARAMETERS

-TimeSpan

The TimeSpan object to format.

Type: TimeSpan
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-Precision

Specifies the number of precision levels to include in the output.

Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: 1
Accept pipeline input: False
Accept wildcard characters: False

-BaseUnit

Specifies the base unit to use for formatting the TimeSpan.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-FullNames

If specified, outputs full unit names instead of abbreviations.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

System.String

NOTES

The formatted string representation of the TimeSpan.

https://psmodule.io/TimeSpan/Functions/Format-TimeSpan/