Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active August 15, 2021 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save quonic/74884d13fbf11c0c38b3349152b93c60 to your computer and use it in GitHub Desktop.
Save quonic/74884d13fbf11c0c38b3349152b93c60 to your computer and use it in GitHub Desktop.
Created this to help convert UNIX time to and from Windows time when interfacing with data from API's that output time in UNIX time.
function ConvertTo-UnixTime {
[OutputType([int64])]
Param(
# Date in UNIX time
[Parameter(Mandatory,
ValueFromPipeline=$true)]
[DateTime]
$DateTime
)
[Math]::Floor([decimal](Get-Date($DateTime).ToUniversalTime() -UFormat "%s"))
}
function ConvertTo-WindowsTime {
[OutputType([DateTime])]
Param(
# Date in UNIX time
[Parameter(Mandatory,
ValueFromPipeline=$true)]
[int64]
$ticks
)
[DateTime]::new(1970, 1, 1, 0, 0, 0, 0, [System.DateTimeKind]::Utc).AddSeconds($ticks).ToLocalTime()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment