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
}
The output looks like a large column of Unix timestamps:
1337329458
Update: I ultimately went with the following:
$ctime = $entry.created
[datetime]$origin = '1970-01-01 00:00:00'
$origin.AddSeconds($ctime)
Using the Get-Date cmdlet with -UFormat %s in PowerShell, it converts the date time to epoch time. To convert the epoch time to date time, use Microsoft . Net class library System. TimeSpan to add the seconds to the epoch time date of January 01, 1970.
Use ParseExact to Convert String to DateTime in PowerShell We have a variable $date , which contains the date in string format. You can check the data type using the GetType() method. Let's convert the string to DateTime. When converting, the date and time format in a string must match the specified format in DateTime.
The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several . NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.
In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.
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)
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.
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
$date = get-date "1/1/1970"
$date.AddSeconds($unixTime).ToLocalTime()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With