PowerShell Get-Date Format ISO 8601

If you’re working with dates and times in PowerShell, you might need to format them according to the ISO 8601 standard. This format is widely used and recognized, making it essential for data exchange and logging. In this tutorial, I will explain the syntax and examples of using the Get-Date cmdlet to format dates in ISO 8601.

To format the current date and time in ISO 8601 using PowerShell, you can use the Get-Date cmdlet with the -Format parameter set to "o". Simply run the command Get-Date -Format "o" to get the date and time in the ISO 8601 format, which includes milliseconds and the time zone offset, ensuring clear and consistent date-time representation.

What is the ISO 8601 Format?

ISO 8601 is an international standard for date and time representations. It ensures clarity and consistency, avoiding ambiguity by using a specific format: YYYY-MM-DDTHH:MM:SS.sssZ. This format is useful in programming and data exchange across different systems.

Syntax of Get-Date for ISO 8601

The Get-Date cmdlet in PowerShell can be used to retrieve the current date and time or format a specified date and time. To format a date in ISO 8601, you can use the -Format parameter with the "o" specifier, which stands for the round-trip date/time pattern.

Get-Date -Format "o"

The Get-Date cmdlet returns the current date and time by default. When used with the -Format parameter, it allows you to specify the output format. The "o" format specifier ensures that the date and time are returned in the ISO 8601 format, including milliseconds and the time zone offset.

The exact output is shown in the screenshot below. I executed the above PowerShell script using VS code.

powershell get-date format iso 8601

Check out Compare Dates Without Time in PowerShell

PowerShell Get-Date Format ISO 8601 Examples

Now, let me show a few examples related to PowerShell Get-Date format ISO 8601.

Example 1: Current Date and Time in ISO 8601

To get the current date and time in ISO 8601 format in PowerShell, you can simply run the following command:

Get-Date -Format "o"

This command will output something like:

2024-09-18T14:23:45.6780000Z

Example 2: Specified Date and Time in ISO 8601

If you need to format a specific date and time, you can create a DateTime object and then format it using Get-Date:

$specifiedDate = Get-Date -Year 2023 -Month 12 -Day 25 -Hour 18 -Minute 30 -Second 00
$specifiedDate.ToString("o")

This will output:

2023-12-25T18:30:00.0000000Z

Example 3: ISO 8601 in UTC

To ensure the date and time are in UTC, you can use the -UFormat parameter with the %s specifier, which returns the Unix timestamp in seconds since the epoch, and then convert it:

(Get-Date).ToUniversalTime().ToString("o")

This command will output the current date and time in UTC in ISO 8601 format:

2024-09-18T14:23:45.6780000Z

Check out How to Get Yesterday’s Date in PowerShell

Example 4: Converting ISO 8601 String to DateTime

If you have a date and time string in ISO 8601 format and need to convert it to a DateTime object, you can use the [DateTime]::Parse method:

$isoDateString = "2024-09-18T14:23:45.6780000Z"
[DateTime]::Parse($isoDateString)

This will convert the string to a DateTime object.

Here is the exact output in the screenshot below:

Get-Date for ISO 8601

Example 5: Using Custom DateTime Formats

You can also use custom date and time formats if you need a slightly different format. For example:

Get-Date -Format "yyyy-MM-ddTHH:mm:ss.fffK"

This will output:

2024-09-18T14:23:45.678Z

Example 6: Formatting Date in Local Time

If you need the date and time in your local time zone but still in ISO 8601 format:

(Get-Date).ToString("o")

This will output the local date and time in ISO 8601 format.

Conclusion

By using the -Format "o" parameter in the PowerShell Get-Date cmdlet, you can easily format dates in ISO 8601. In this tutorial, I explained everything about the PowerShell Get-Date Format ISO 8601 with examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.