Understanding and working with Epoch time in PowerShell is essential when dealing with date and time operations in scenarios like JWT tokens, assertions, or system logs. This guide provides examples of how to manipulate Epoch time and convert it into human-readable formats using PowerShell.
What Is Epoch Time?
Epoch time, also known as Unix time, POSIX time, or Unix timestamp, is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), excluding leap seconds. It is widely used in programming and systems as a standard representation of time.
In JWT tokens, Epoch time is commonly used for claims such as iat
(issued at), nbf
(not before), and exp
(expiration). Below is an example of Epoch time in a JWT token:
{
"typ": "JWT",
"alg": "RS256",
"x5t": "3PaK4EfYBNQu3CtjYsa3YmhqQ5E0",
"kid": "3PaK4EfYBNQu3CtjYsa3YmhqQ5E0",
"aud": "00000003-0000-0000-c000-000000000000",
"iss": "https://sts.windows.net/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/",
"iat": 1729784492,
"nbf": 1729784492,
"exp": 1729787119
}
Working with Date and Time in PowerShell
Get Current Date and Time in UTC
To get the current date and time in UTC:
$utc = (Get-Date).ToUniversalTime()
Convert to ISO 8601 Standard Format
To convert the UTC date to ISO 8601 format:
Get-Date ((Get-Date).ToUniversalTime()) -Format o
Adding and Subtracting Time
You can add or subtract time from the current date in PowerShell using the following commands:
Add Time
Get-Date ($utc).AddYears(1) -Format o
Get-Date ($utc).AddMonths(1) -Format o
Get-Date ($utc).AddDays(1) -Format o
Get-Date ($utc).AddHours(1) -Format o
Get-Date ($utc).AddMinutes(1) -Format o
Get-Date ($utc).AddSeconds(1) -Format o
Subtract Time
Get-Date ($utc).AddYears(-1) -Format o
Get-Date ($utc).AddMonths(-1) -Format o
Get-Date ($utc).AddDays(-1) -Format o
Get-Date ($utc).AddHours(-1) -Format o
Get-Date ($utc).AddMinutes(-1) -Format o
Get-Date ($utc).AddSeconds(-1) -Format o
Convert Epoch Time to Readable Date
To convert Epoch time (e.g., exp
claim in JWT) into a human-readable date:
$epoch = "1729838496"
(Get-Date -Date "1/1/1970").AddSeconds($epoch)
Generate Epoch Time
To generate Epoch time from the current date:
Get-Date $((Get-Date).ToUniversalTime()) -UFormat %s
To generate Epoch time that is 12 hours ahead:
Get-Date $((Get-Date).AddHours(12).ToUniversalTime()) -UFormat %s
Useful Online Tools
If you need a quick conversion tool, you can use:
Conclusion
PowerShell makes it easy to work with Epoch time for various use cases, including JWT token claims, assertions, and system logs. By leveraging the commands above, you can manipulate and convert Epoch time efficiently, ensuring accurate time handling in your scripts and applications.