Unix Timestamp in PowerShell

PowerShell can use .NET directly: [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() for the current timestamp and [DateTimeOffset]::FromUnixTimeSeconds(ts) to convert back.

Get & convert epoch time in PowerShell

[DateTimeOffset]::FromUnixTimeSeconds(1700000000).UtcDateTime # 2023-11-14T22:13:20.000Z
[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
Gotcha: Get-Date returns a DateTime in local time; go through [DateTimeOffset] to keep the conversion zone-correct.

Frequently asked questions

How do I get the current Unix timestamp in PowerShell?
[DateTimeOffset]::UtcNow.ToUnixTimeSeconds().
How do I convert a Unix timestamp in PowerShell?
[DateTimeOffset]::FromUnixTimeSeconds(ts).UtcDateTime.

Other languages

Copied