Convert Unix time with PowerShell

2019-01-06 23:29发布

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 I would like to do is somehow convert that into "human time". I have removed some of the other SQL queries for ease of reading.

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
}

I went with the following:

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

The output looks like a large column of Unix timestamps:

1337329458

6条回答
混吃等死
2楼-- · 2019-01-06 23:54
$ctime = $entry.created
[datetime]$origin = '1970-01-01 00:00:00'
$origin.AddSeconds($ctime)
查看更多
时光不老,我们不散
3楼-- · 2019-01-06 23:55

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

FromUnixTimeMilliseconds is also available

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

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#Sortable

查看更多
我只想做你的唯一
4楼-- · 2019-01-07 00:06

http://codeclimber.net.nz/archive/2007/07/10/convert-a-unix-timestamp-to-a-.net-datetime.aspx

You can easily reproduce this in PowerShell.

$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$whatIWant = $origin.AddSeconds($unixTime)
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-07 00:12
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.
查看更多
Deceive 欺骗
6楼-- · 2019-01-07 00:12

A Simple one liner :

(Get-Date "1970-01-01 00:00:00.000Z") + ([TimeSpan]::FromSeconds($unixTime))
查看更多
够拽才男人
7楼-- · 2019-01-07 00:13
$date = get-date "1/1/1970"
$date.AddSeconds($unixTime).ToLocalTime()
查看更多
登录 后发表回答