问题
How can I find the time in milliseconds using PowerShell?
回答1:
In PowerShell you can cast a time value to a timespan and call the TotalMilliseconds method:
([TimeSpan]"00:05:00").TotalMilliseconds # Returns 300000
([TimeSpan] (Get-Date).ToShortTimeString()).TotalMilliseconds # Returns total milliseconds from 00:00:00 till now
回答2:
You can get the full date with milliseconds with the following:
Get-Date -Format HH:mm:ss.fff
回答3:
The question suggests finding a given datetime in milliseconds (Microsoft epoch time). This is easily solved with:
[Math]::Round((Get-Date).ToFileTime()/10000)
or
[Math]::Round((Get-Date).ToFileTimeUTC()/10000)
To convert this to Unix epoch time in seconds:
[Math]::Round((Get-Date).ToFileTime() / 10000000 - 11644473600)
Where 11644473600 is the number of elapsed seconds between the Microsoft epoch (January 1, 1601 A.D. (C.E.)) and the Unix epoch (January 1, 1970, 12AM UTC/GMT)
https://msdn.microsoft.com/en-us/library/system.datetime.tofiletime(v=vs.110).aspx
回答4:
If you need (sub-)millisecond resolution, check out System.Diagnostics.Stopwatch.
$stopwatch = New-Object System.Diagnostics.Stopwatch
$stopwatch.Start()
$stopwatch.Stop()
$stopwatch
来源:https://stackoverflow.com/questions/9060269/find-time-in-milliseconds-using-powershell