How to Format a DateTime in PowerShell
-
Use the
-format
Option to Format a Date and Time in PowerShell -
Use
(Get-Date).ToString
to Format a Date and Time in PowerShell -
Select From All Date and Time Formatting Styles Using
GetDateTimeFormats()
in PowerShell -
Format the Date and Time to Use in an
HTTP header
in PowerShell -
Use the
UFormat
Option to Format a Date and Time in PowerShell - Conclusion
Get-Date
is a cmdlet in PowerShell, a task-based command-line shell and scripting language built on .NET
. This cmdlet is primarily used to retrieve the current date and time from the system where the PowerShell is running.
This tutorial will introduce different methods to format a date
and time
in PowerShell. You can view the current date and time of the system using a cmdlet Get-Date
.
Get-Date
Output:
The above output is the default formatting for displaying the date
and time
. We will look at different examples to display the date and time in other possible formats.
Use the -format
Option to Format a Date and Time in PowerShell
The -Format
option in PowerShell is a parameter used primarily with the Get-Date
cmdlet. It allows users to specify a custom format for displaying date and time information.
This option is essential for scripts and commands where date and time need to be presented in a specific format, whether for logging, user interface display, or data processing.
The following command displays the current date
and time
in the format of Month-Day-Year
and Hour:Minute:Second
, respectively.
Get-Date -Format "MM/dd/yyyy HH:mm:ss"
Initially, the Get-Date
cmdlet is invoked, which is PowerShell’s way of fetching the current system date and time.
Following this, we apply the -Format
parameter, a crucial aspect of the command, which dictates how this DateTime
data is presented. The string "MM/dd/yyyy HH:mm:ss"
within the parameter serves as a format specifier: MM
formats the month as a two-digit number, dd
does the same for the day, yyyy
represents the year in a four-digit format, HH
formats the hour in a 24-hour clock, mm
for minutes, and ss
for seconds.
By executing this command, we effectively instruct PowerShell to output the current date and time in a very specific, human-readable format, catering to common conventions and readability.
Output:
When you want to format the date only (without time) using the -Format
option in PowerShell’s Get-Date
cmdlet, you would specify a format string that includes only the date components.
Get-Date -Format "dd/MM/yyyy"
Output:
Use (Get-Date).ToString
to Format a Date and Time in PowerShell
One of the versatile ways to format DateTime
is by using the .ToString()
method. This method converts the DateTime
object into a string representation, allowing for custom formats and easy readability.
(Get-Date).ToString("dd/MM/yyyy HH:mm:ss")
In this example, we start by retrieving the current date and time using Get-Date
. Then, we immediately use the .ToString()
method on this object.
The format string "dd/MM/yyyy HH:mm:ss"
instructs PowerShell to format the date with the day and month in two digits, followed by a four-digit year, and the time in hours (24-hour format), minutes, and seconds.
This process effectively converts the current DateTime
into a string that represents the date and time in our specified format, making it more readable and standardized.
Output:
Select From All Date and Time Formatting Styles Using GetDateTimeFormats()
in PowerShell
The GetDateTimeFormats()
method is used to obtain an array of string representations of a DateTime
object in different formats.
To view all the date
and time
formats, you can use the below command. It displays a large string array of formatting styles for date and time.
(Get-Date).GetDateTimeFormats()
Output:
You can select one of the formatting styles from the array list with []
. For example:
(Get-Date).GetDateTimeFormats()[7]
Here, Get-Date
retrieves the current date and time as a DateTime
object. Next, we call the .GetDateTimeFormats()
method on this object, which yields an array of various date and time formats.
By using [7]
, we specifically access the eighth element in this array, given that indexing starts at 0. This element corresponds to one of the standard date formats defined in the .NET
framework, influenced by the system’s regional settings
Output:
Format the Date and Time to Use in an HTTP header
in PowerShell
You can use the format specifier R
or r
to format the date and time for use in an HTTP header
. It displays the date and time in RFC1123
format.
The RFC1123 Format
is a specific date-time format derived from the Internet standard RFC1123
. It represents dates in a format like Mon, 15 Jun 2009 20:45:30 GMT
.
This format is crucial in web communications, especially in HTTP headers
, because it ensures that date and time information is consistently understood across different systems and locations.
Get-Date -Format R
Output:
OR
(Get-Date).ToString("R")
Output:
Both Get-Date -Format R
and (Get-Date).ToString("R")
in PowerShell are used to format dates, specifically to the RFC1123
date-time format, which is commonly used in HTTP headers
and other web standards.
While Get-Date -Format R
provides a quick and direct output, (Get-Date).ToString("R")
allows for additional manipulations of the DateTime
object before formatting it.
Use the UFormat
Option to Format a Date and Time in PowerShell
In PowerShell, the UFormat
method is a less commonly used but still powerful approach to formatting DateTime
objects. It’s inspired by the Unix/Linux world, where the strftime
function in C language is widely used for formatting time.
This method offers a different set of format specifiers compared to the standard .NET DateTime
formatting options and can be particularly useful when working in cross-platform environments or when a specific Unix-style timestamp is required.
$currentDateTime = Get-Date -UFormat "%d/%m/%Y %H:%M:%S"
$currentDateTime
In this example, we use Get-Date
to fetch the current date and time. By applying the -UFormat
option, we specify that we want to format this DateTime
in a Unix-style format.
The format string %d/%m/%Y %H:%M:%S
tells PowerShell to format the date as day/month/year and time in a 24-hour format with hours, minutes, and seconds. The formatted string is stored in $currentDateTime
.
This method is particularly useful when we want to align the date-time format with Unix/Linux systems or in cross-platform scripting scenarios.
Output:
Conclusion
In this article, we explored several methods to format date and time in PowerShell. Starting with the Get-Date
cmdlet, we showcased its fundamental role in fetching the system’s current date and time.
We then examined the -Format
option, a versatile and essential tool for customizing date and time displays in scripts and automation tasks. Our exploration further included the .ToString()
method, offering a flexible approach for converting DateTime objects into custom string formats, ideal for unique formatting needs.
Additionally, we discussed the GetDateTimeFormats()
method, which provides a wide array of pre-defined formatting options useful for adhering to standard or regional formatting preferences. Lastly, we delved into the UFormat
option, a method inspired by Unix/Linux systems, beneficial for scripts interacting with such environments or requiring Unix-style timestamps.