Powershell: Convert UNIX Timestamp to DateTime

Hi,

simple one liner to convert an UNIX Timnestap to DataTime object.

PS D:\> $oUNIXDate=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds(1537615930))
PS D:\> $oUNIXDate
Samstag, 22. September 2018 11:32:10

Michael

6 thoughts on “Powershell: Convert UNIX Timestamp to DateTime”

    1. Unable to convert the number # 1589997744995 .

      $oUNIXDate=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds(1589997744995))
      Exception calling “FromSeconds” with “1” argument(s): “TimeSpan overflowed because the duration is too long.”
      At line:1 char:1
      + $oUNIXDate=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds(1589 …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : OverflowException

  1. I was able to get it to work, using what another separate article suggested for another language by removing the last 3 digits. In the parentheses where the 13 character integer string is, add “/ 1000” at the end, then run it. It worked for me.

    What I ran in PS:
    $oUNIXDate=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds(1627505005033 / 1000))

    What I got when I ran $oUNIXDate:
    Wednesday, July 28, 2021 8:43:25 PM

Leave a Reply