In the previous tip we explained how you convert date and time displayed as ticks to a true DateTime format. However, there are two different time formats involving ticks, and here is an overview of how you can convert numeric datetime information:
PS> $date = Get-Date -Date '2017-02-03 19:22:11' PS> $ticks = $date.Ticks PS> $ticks 636217465310000000 PS> [DateTime]$ticks Friday, February 3, 2017 19:22:11 PS> [DateTime]::FromBinary($ticks) Friday, February 3, 2017 19:22:11 PS> [DateTime]::FromFileTime($ticks) Friday, February 3, 3617 20:22:11 PS> [DateTime]::FromFileTimeUtc($ticks) Friday, February 3, 3617 19:22:11
As you see, converting ticks to DateTime is equivalent of running the FromBinary() static method. But what does FromFileTime() do? It seems to beam you in the far future.
This example shows what’s going on:
PS> $date1 = [DateTime]::FromBinary($ticks) PS> $date2 = [DateTime]::FromFileTime($ticks) PS> $date2 - $date1 Days : 584388 Hours : 1 Minutes : 0 Seconds : 0 Milliseconds : 0 Ticks : 504911268000000000 TotalDays : 584388,041666667 TotalHours : 14025313 TotalMinutes : 841518780 TotalSeconds : 50491126800 TotalMilliseconds : 50491126800000 PS> ($date2 - $date1).Days / 365.25 1599,96714579055
FromFileTime() simply adds 1601 years (the calculated difference is a little off due to leap years). Some parts of Windows (like Active Directory) start their calendar at Jan 1, 1601. For these, use FromFileTime() to get the accurate date and time.