Converting date and time to Epoch (Unix) time, and Epoch time back to human readable date.
1. Regular time to Epoch time
# Get current date and time PS C:\> $DateTime = Get-Date # Convert to Epoch PS C:\> $EpochTime = Get-Date $DateTime -UFormat %s # $EpochTime # 1616758238.2857
2. Epoch to Human Readable Date
# Get the actual Epoch Timestamp to work with PS C:\> $EpochTime = Get-Date -UFormat %s # Convert to regular date PS C:\> $DateTime = (([System.DateTimeOffset]::FromUnixTimeSeconds($EpochTime)).DateTime).ToString() # $DateTime # 26/03/2021 11:30:38
Just need to alert you that you’re calling $Now instead of $DateTime in the first part of the code…
Thank you, Goran. Good catch. I’ve fixed the code.
Thank you for this. Really helped me.