Reputation: 9664
New-Timespan takes no "MilliSeconds" parameter, how do you create a TimeSpan from milliseconds?
Upvotes: 8
Views: 11220
Reputation: 27423
10,000 ticks (integer, not float or string) is 1 millisecond:
[timespan]10000
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 1
Ticks : 10000
TotalDays : 1.15740740740741E-08
TotalHours : 2.77777777777778E-07
TotalMinutes : 1.66666666666667E-05
TotalSeconds : 0.001
TotalMilliseconds : 1
Upvotes: 0
Reputation: 16606
As of PowerShell v7.3 there is a -Milliseconds
parameter of New-TimeSpan
...
PS> New-TimeSpan -Milliseconds 10
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 10
Ticks : 100000
TotalDays : 1.15740740740741E-07
TotalHours : 2.77777777777778E-06
TotalMinutes : 0.000166666666666667
TotalSeconds : 0.01
TotalMilliseconds : 10
Otherwise, use the FromMilliseconds
static method of the TimeSpan
structure...
PS> [TimeSpan]::FromMilliseconds(10)
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 10
Ticks : 100000
TotalDays : 1.15740740740741E-07
TotalHours : 2.77777777777778E-06
TotalMinutes : 0.000166666666666667
TotalSeconds : 0.01
TotalMilliseconds : 10
A TimeSpan
ultimately represents its duration as a number of Ticks
, so if you prefer to think of it that way you can multiply the number of milliseconds by the TicksPerMillisecond
constant and pass that to the constructor that accepts the number of ticks (there is no FromTicks()
method)...
PS> New-Object -TypeName 'TimeSpan' -ArgumentList (10 * [TimeSpan]::TicksPerMillisecond)
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 10
Ticks : 100000
TotalDays : 1.15740740740741E-07
TotalHours : 2.77777777777778E-06
TotalMinutes : 0.000166666666666667
TotalSeconds : 0.01
TotalMilliseconds : 10
PS> [TimeSpan]::new(10 * [TimeSpan]::TicksPerMillisecond)
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 10
Ticks : 100000
TotalDays : 1.15740740740741E-07
TotalHours : 2.77777777777778E-06
TotalMinutes : 0.000166666666666667
TotalSeconds : 0.01
TotalMilliseconds : 10
Upvotes: 16
Reputation: 29
Positive: [timespan]'0:0:0.001' or [timespan]'00:00:00:00.001'
Negative: [timespan]'-0:0:0.001' or [timespan]'-00:00:00:00.001'
Specifying 4 or 5 [int32] numbers get interpreted as (optionally days,) hours, minutes, seconds and milliseconds.
For a more complete answer see https://devblogs.microsoft.com/scripting/weekend-scripter-understanding-timespan-objects/ and scroll down to TimeSpan constructors.
Each time unit specified must stay within its usual limits (0-23 for hours, 0-59 for minutes and seconds, 0-999 for milliseconds). The days range (if specified) is 0-10675199.
The highest possible [timespan] value appears to be [timespan]'10675199:2:48:5.477' (verified on PowerShell 5.1 and pwsh 7.1.1).
Upvotes: 2
Reputation: 1
$Start_DateTime = Get-Date -format HH:mm:ss.fff
...... other commnads
$Finish_DateTime = Get-Date -format HH:mm:ss.fff
$TimeDiff = New-TimeSpan $Start_DateTime $Finish_DateTime
IF ($TimeDiff.Seconds -lt 0)
{ $Hrs = ($TimeDiff.Hours) + 23
$Mins = ($TimeDiff.Minutes) + 59
$Secs = ($TimeDiff.Seconds) + 59
$Milliseconds = ($TimeDiff.Milliseconds) + 59
}
ELSE
{ $Hrs = $TimeDiff.Hours
$Mins = $TimeDiff.Minutes
$Secs = $TimeDiff.Seconds
$Milliseconds = $TimeDiff.Milliseconds
}
Upvotes: -1