Convert Unix time with PowerShell

后端 未结 7 1867
南方客
南方客 2020-11-28 14:58

I am parsing an SQLite database using the PowerShell SQLite module, and a couple of the return values are created and modified, both of which are in Unix time.

What

相关标签:
7条回答
  • 2020-11-28 15:35

    See Convert a Unix timestamp to a .NET DateTime.

    You can easily reproduce this in PowerShell.

    $origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
    $whatIWant = $origin.AddSeconds($unixTime)
    
    0 讨论(0)
  • 2020-11-28 15:39

    Use:

    (([System.DateTimeOffset]::FromUnixTimeSeconds($unixTime)).DateTime).ToString("s")
    

    FromUnixTimeMilliseconds is also available.

    ToString("s"): Sortable: "The pattern reflects a defined standard (ISO 8601)"

    Ref.: Standard Date and Time Format Strings, The Sortable ("s") Format Specifier

    0 讨论(0)
  • 2020-11-28 15:42
    $date = get-date "1/1/1970"
    $date.AddSeconds($unixTime).ToLocalTime()
    
    0 讨论(0)
  • 2020-11-28 15:43
    $ctime = $entry.created
    [datetime]$origin = '1970-01-01 00:00:00'
    $origin.AddSeconds($ctime)
    
    0 讨论(0)
  • 2020-11-28 15:46
    Function Convert-FromUnixDate ($UnixDate) {
       [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($UnixDate))
    }
    
    $niceTime = Convert-FromUnixDate $ctime
    
    PS C:\> $niceTime
    
    Friday, 18 May 2012 8:24:18 p.m.
    
    0 讨论(0)
  • 2020-11-28 15:48

    A simple one-liner:

    (Get-Date "1970-01-01 00:00:00.000Z") + ([TimeSpan]::FromSeconds($unixTime))
    
    0 讨论(0)
提交回复
热议问题