How to Get Date and Time in 24-Hour Format in PowerShell?

One of my team members was recently required to get date in 24-hour format in PowerShell. I suggested a solution. If you’re working with PowerShell and need to display the current date and time in a 24-hour format, then read this complete tutorial.

To display the current date and time in 24-hour format using PowerShell, you can use the Get-Date cmdlet with the -Format parameter. The command Get-Date -Format "MM/dd/yyyy HH:mm:ss" will output the date and time in the desired format, where MM represents the month, dd the day, yyyy the year, HH the hour in 24-hour format, mm the minutes, and ss the seconds.

PowerShell Get-Date 24 Hour Format

The Get-Date cmdlet in PowerShell is used to retrieve the current date and time or a specified date and time. It can also format the output in various ways, including 24-hour format.

The basic syntax for Get-Date is:

Get-Date

This command returns the current date and time. However, to customize the format, we need to use the -Format parameter.

Format Date and Time in 24-Hour Format

To display the date and time in a 24-hour format, you can use the -Format parameter with a specific format string. Here’s the syntax:

Get-Date -Format "MM/dd/yyyy HH:mm:ss"

In this format string:

  • MM represents the month.
  • dd represents the day.
  • yyyy represents the year.
  • HH represents the hour in 24-hour format.
  • mm represents the minutes.
  • ss represents the seconds.

Here is the output in the screenshot below:

powershell get-date 24 hour format

Examples

Let’s dive into some practical examples to see how this works.

Example 1: Display Current Date and Time

To display the current date and time in 24-hour format, you can use the following command:

Get-Date -Format "MM/dd/yyyy HH:mm:ss"

This will output something like:

09/15/2024 14:35:47

Check out How to Format Date in PowerShell

Example 2: Store the Formatted Date and Time in a Variable

You might want to store the formatted date and time in a variable for later use. Here’s how you can do it:

$currentDateTime = Get-Date -Format "MM/dd/yyyy HH:mm:ss"
Write-Output $currentDateTime

This will store the formatted date and time in the $currentDateTime variable and then print it.

Here is the output in the screenshot below:

Get Date and Time in 24-Hour Format in PowerShell

Example 3: Using Custom Date and Time

If you need to work with a specific date and time, you can create a DateTime object and format it. For example, let’s say you want to format the date and time for July 4th, 2025, at 18:30:00:

$customDate = Get-Date "07/04/2025 18:30:00"
$customDate.ToString("MM/dd/yyyy HH:mm:ss")

This will output:

07/04/2025 18:30:00

Check out Get Date Without Time in PowerShell

Advanced Formatting with UFormat

Another method to format the date and time in PowerShell is by using the -UFormat parameter, which is based on Unix strftime formatting. Here’s how you can use it:

Get-Date -UFormat "%m/%d/%Y %H:%M:%S"

In this format string:

  • %m represents the month.
  • %d represents the day.
  • %Y represents the year.
  • %H represents the hour in 24-hour format.
  • %M represents the minutes.
  • %S represents the seconds.

This approach is particularly useful if you’re familiar with Unix-style date formatting.

Conclusion

In this tutorial, I have explained how to get date and time in 24-Hour format in PowerShell. Whether you’re logging events, scheduling tasks, or simply need to display the current date and time, understanding how to format the output in a 24-hour format is crucial.

By using the -Format or -UFormat parameters, you can easily customize the date and time in a 24-hour format.

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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