Converting Unix Time Using PowerShell

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

What I would like to do is transform it into "human time" in one way or another. I deleted some other SQL queries for readability.

Import-Module SQLite
mount-sqlite -name GoogleDrive -dataSource E:\Programming\new.db
$cloud_entry = Get-ChildItem GoogleDrive:\cloud_entry

foreach ($entry in $cloud_entry)
{
    $entry.created
}

The output looks like a large column of Unix timestamps:

1337329458

Update. I ended up choosing the following:

$ctime = $entry.created
[datetime]$origin = '1970-01-01 00:00:00'
$origin.AddSeconds($ctime)
+7
source share
6 answers

See Converting 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)
+21
source
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.
+15
source
$date = get-date "1/1/1970"
$date.AddSeconds($unixTime).ToLocalTime()
+6
source

Simple single line:

(Get-Date "1970-01-01 00:00:00.000Z") + ([TimeSpan]::FromSeconds($unixTime))
+5
source

Using:

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

FromUnixTimeMilliseconds also available.

ToString ("s"): Sortable: "The template reflects a specific standard (ISO 8601)"

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

+4
source
$ctime = $entry.created
[datetime]$origin = '1970-01-01 00:00:00'
$origin.AddSeconds($ctime)
+1
source

All Articles