How to calculate current date as number of milliseconds since 1601-01-01 with PowerShell

StackOverflow https://stackoverflow.com/questions/16808863

  •  30-05-2022
  •  | 
  •  

Question

How to calculate number of miliseconds since 1601-01-01 for today's date with windows PowerShell script? I need this to build correct LDAP queries.

Was it helpful?

Solution

A DateTime structure contains method ToFileTime. As per documentation,

A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC).

Thus, going from ns (10e-9) to ms (10e-3) is simple arithmetics. Just mind that the counter counts 100 ns blocks, not 1 ns blocks. The value is stored as an Int64, so no type conversion is needed. Like so,

PS C:\> (Get-Date).ToFileTime()
130142949169114886
PS C:\> (Get-Date).ToFileTime().GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int64                                    System.ValueType

OTHER TIPS

Full agree with @vonPryz answer. Just for fun You can find the number of 100-nanosecond in the tick property of the Powershell System.DateTime. But this tick is not from "01/01/1600" but from ([datetime]::MinValue) "01/01/0001".

try :

$a = ([datetime]::Now).Ticks  - ([datetime]("01/01/1600 12:00")).Ticks
[datetime]::FromFileTimeUtc($a)

It will be correct:

(Get-Date).ToFileTime()/10000

It's scary to even think what will happen to us if the above simple solution (Get-Date).ToFileTime() gives an error of 10,000 times

4205233 years. It's terrible

$a = ([datetime]::Now).Ticks
$secTimer=1
Start-Sleep -Seconds $secTimer
$b = ([datetime]::Now).Ticks
$c=$b-$a
'ticks={0} == {1} sec and {2} ticks' -f $c,[int](Get-Date  $c -Format "ss"),[int](Get-Date  $c -Format "fffffff")
$TicksPerSec = $c/$secTimer
'ticks per second = {0}' -f ($c/$secTimer)
echo "`n"
$Year=1601;$Month=1;$date=1;$hour=0;$minutes=0;$Seconds=0;$mSeconds=0;
$Ticks1601 = New-Object DateTime $Year, $Month, $date, $hour, $minutes, $Seconds, $mSeconds
'Ticks on Jan 1 1601 00:00:00   =  {0}' -f $Ticks1601.Ticks
$TicksNow = ([datetime]::Now).Ticks
$time=$TicksNow-$Ticks1601
'after Jan 1 1601 00:00:00'
'  milliseconds {0}' -f ($time.Ticks/$TicksPerSec*1000)
$seconds=$time.Ticks/$TicksPerSec
'  seconds =    {0}' -f $seconds
$min=$seconds/60
'  minutes =    {0}' -f $min
$hours=$min/60
'  hours =      {0}' -f $hours
$days=$hours/24
'  days =        {0}' -f $days
$years=$days/364.75
'  years =      {0}' -f $years

echo "`nand`n"
$ms=(Get-Date).ToFileTime()
'simple ToFileTime() after Jan 1 1601 00:00:00'
'  milliseconds {0}' -f $ms
$years=$ms/1000/60/60/24/364.75
'  years =      {0}' -f $years
'???'
echo "`n"
$Year=1;$Month=1;$date=1;$hour=0;$minutes=0;$Seconds=0;$mSeconds=0;
$time = New-Object DateTime $Year, $Month, $date, $hour, $minutes, $Seconds, $mSeconds
'Ticks on Jan 1 0001 00:00:00   =  {0}' -f $time.Ticks
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top