6

New-Timespan takes no "MilliSeconds" parameter, how do you create a TimeSpan from milliseconds?

Marius
  • 8,778
  • 7
  • 46
  • 70

3 Answers3

12

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 multiple 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
Lance U. Matthews
  • 14,475
  • 6
  • 41
  • 65
1

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).

G-e-V-e
  • 19
  • 3
  • _So_ close. `[TimeSpan]::MaxValue.ToString()` yields `10675199.02:48:05.4775807`, so you're about a half millisecond (or 5,807 [ticks](https://docs.microsoft.com/dotnet/api/system.timespan.ticks)) off on the maximum `TimeSpan` value; you can see this with `[TimeSpan]::MaxValue - [TimeSpan] '10675199:2:48:5.477'`. – Lance U. Matthews Feb 12 '21 at 01:53
-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
}