Find time in milliseconds?
Asked Answered
R

7

30

How can I find the time in milliseconds using PowerShell?

Radtke answered 30/1, 2012 at 6:42 Comment(0)
R
11

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
Rempe answered 30/1, 2012 at 6:58 Comment(3)
can you refer me any good book for learn powershell scripting ? i'm really thankfullRadtke
If you need to cast an initial value with sub-second precision you can use the following: ( [TimeSpan]::FromSeconds(0.9780526) ).TotalMinutes # return 0.0163Flatt
The 2nd line gives me Cannot convert value "11:20 AM" to type "System.TimeSpan". Error: "String was not recognized as a valid TimeSpan."Jeri
C
58

You can get the full date with milliseconds with the following:

Get-Date -Format HH:mm:ss.fff
Calle answered 15/7, 2014 at 14:58 Comment(1)
He used markdown for the code. It starts and ends with a back-tick. `Hole
G
18

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

Grillo answered 6/7, 2017 at 15:56 Comment(1)
Microsoft counts 100 nanosecond intervals since its epoch, this needs to be converted into milliseconds.Grillo
R
11

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
Rempe answered 30/1, 2012 at 6:58 Comment(3)
can you refer me any good book for learn powershell scripting ? i'm really thankfullRadtke
If you need to cast an initial value with sub-second precision you can use the following: ( [TimeSpan]::FromSeconds(0.9780526) ).TotalMinutes # return 0.0163Flatt
The 2nd line gives me Cannot convert value "11:20 AM" to type "System.TimeSpan". Error: "String was not recognized as a valid TimeSpan."Jeri
H
10

This should work:

[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()

returns

1624186843769
Hypodermis answered 20/6, 2021 at 11:4 Comment(0)
D
9

If you need (sub-)millisecond resolution, check out System.Diagnostics.Stopwatch.

$stopwatch = New-Object System.Diagnostics.Stopwatch
$stopwatch.Start()
$stopwatch.Stop()
$stopwatch
Desist answered 30/1, 2012 at 15:24 Comment(0)
E
0

Vignesh Narendran's answer gives the Epoch milliseconds time in UTC.

If you want it in your computer's time zone, as a string:

(Get-Date -UFormat %s).Replace('.', '').Substring(0, 13)

As a number:

[Long](Get-Date -UFormat %s).Replace('.', '').Substring(0, 13)

PowerShell 7.1 added a -AsUTC switch for Get-Date if you want UTC.

Exile answered 31/12, 2022 at 0:59 Comment(1)
Powershell 5.1 is in local time, and powershell 7 is in UTC.Jeri
J
0

10,000 ticks is 1 millisecond. (1 tick is 100 nanoseconds.) Whether it has to be in UTC is another question. Microsoft ticks and filetime and unix time all count from different dates.

(get-date).ticks/10000 # local time (I'm EST -5:00)

63808255598664.8


([datetime]'1/1/2023').ticks/10000

63808128000000


([timespan]10000).TotalMilliseconds

1


([datetimeoffset]'1/1/2023').ticks/10000      # from 1/1/0001 local time

63808128000000


([datetimeoffset]'1/1/2023').ToUnixTimeMilliseconds() # from 1/1/1970 utc

1672549200000


([datetimeoffset]'1/1/2023').ToFileTime()/10000 # milliseconds from 1/1/1601 utc

13317022800000



([datetimeoffset]'1/1/0001').ticks

0


([datetimeoffset]'12/31/1969 7pm').ToUnixTimeMilliseconds()

0


([datetimeoffset]'12/31/1600 7pm').ToFileTime() # ticks

0
Jeri answered 2/1, 2023 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.