Last active
July 11, 2025 17:37
-
-
Save markekraus/46f8db6e5ca06d67744ecc27a900770f to your computer and use it in GitHub Desktop.
PowerShell Unix Epoch Conversions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertFrom-Epoch { | |
[CmdletBinding()] | |
[OutputType([datetime])] | |
param ( | |
[Parameter(ValueFromPipeline, Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[long] | |
$Epoch, | |
[switch] | |
$FromMilliseconds, | |
[switch] | |
$AsLocalTime | |
) | |
process { | |
if ($FromMilliseconds.IsPresent) { | |
$offset = [System.DateTimeOffset]::FromUnixTimeMilliseconds($Epoch) | |
} | |
else { | |
$offset = [System.DateTimeOffset]::FromUnixTimeSeconds($Epoch) | |
} | |
if ($AsLocalTime.IsPresent) { | |
return $offset.UtcDateTime.ToLocalTime() | |
} | |
$offset.UtcDateTime | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertTo-Epoch { | |
[CmdletBinding()] | |
[OutputType([long])] | |
param ( | |
[Parameter(ValueFromPipeline, Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[datetime] | |
$Date, | |
[switch] | |
$AsMilliseconds | |
) | |
process { | |
$offset = [System.DateTimeOffset]::New($Date.ToUniversalTime()) | |
if ($AsMilliseconds.IsPresent) { | |
return $offset.ToUnixTimeMilliseconds() | |
} | |
$offset.ToUnixTimeSeconds() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment