Skip to content

Instantly share code, notes, and snippets.

@markekraus
Last active July 11, 2025 17:37
Show Gist options
  • Save markekraus/46f8db6e5ca06d67744ecc27a900770f to your computer and use it in GitHub Desktop.
Save markekraus/46f8db6e5ca06d67744ecc27a900770f to your computer and use it in GitHub Desktop.
PowerShell Unix Epoch Conversions
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
}
}
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