Simplifying Unix Epoch Timestamps in PowerShell Using DateTimeOffset
Handling Unix Epoch timestamps in PowerShell can be cumbersome, but with DateTimeOffset, you can streamline conversions with minimal effort.
If you’ve ever dealt with Unix Epoch time in PowerShell, you know it can be a hassle. By default, Get-Date doesn’t output or accept Epoch time, forcing users to manually calculate the seconds between 1970 and their target date.
PS C:\> # Examples of Previous ways to calculate current Unix Time PS C:\> [int][double]::Parse((Get-Date -UFormat %s)) 1724142672 PS C:\> [System.Math]::Truncate((Get-Date -UFormat %s)) 1724142720 Fortunately, there’s a better way. The .NET DateTimeOffset class provides an efficient method to handle Unix Epoch timestamps directly in PowerShell.