PowerShell Get-Date Format

In this article, we will discuss and use PowerShell's Get-Date Format (or Date Format) command. Date and times properties are used several times throughout the PowerShell scripts. Using the Get-Date cmdlet, we can retrieve the current date of our machine. The default format of date is defined on the local system on which we run the command. There are several formats and parameters are used with the Get-Date command. Here, we will only focus on the formatting and display the different date results.

Before performing all the Get-Date cmdlet operations on the PowerShell, we must know how to start the PowerShell tool (application).

Start PowerShell as an Administrator

There are various ways to start PowerShell as an Administrator; some of the basic ways are mentioned below:

1. Run PowerShell as an Administrator using Windows's search bar

Step 1: Windows 10 operating system comes with a search field in the taskbar. Point the cursor in the search field and type "powershell" (or PowerShell).

PowerShell Get-Date Format

Step 2: Based on the search result, right-click on the Windows PowerShell and Run as Administrator. It will take you to Windows PowerShell's as administrator mode.

PowerShell Get-Date Format

2. Run PowerShell as an Administrator using Run window

Step 1: Press the Windows + R keys simultaneously, and it will open a Run dialog box. Now, type the PowerShell in the Run dialog box and click on the OK button.

PowerShell Get-Date Format

Step 2: Step 1 opens the normal PowerShell windows for the current user. Now, on the PowerShell windows, type the command start-process powershell -verb runas and press the "enter" key.

PowerShell Get-Date Format

Step 3: The above command asks you to open Windows PowerShell as an administrator mode; click OK to allow, and it will bring the Windows PowerShell to the administrator.

PowerShell Get-Date Format

Date Formatting Parameters

Followings are the different types of date format in PowerShell:

  • DisplayHint
  • Format
  • UFormat

When to use Get-Date

The Get-Date command returns a DateTime object that shows the current date or date you specify on your local device. Let's see the default format of the system when we use the Get-Date command.

PowerShell Get-Date Format

The Get-Date command returns a date and time value in string type of various UNIX and .NET formats and moves the string to other cmdlets.

The format of output shown on your shell's screen is based on your formatted computer settings. To view your device date settings, use the (Get-Culture).DateTimeFormat command; it will return different date and time formats.


PowerShell Get-Date Format

How to use the Get-Date command

The command Get-Date returns the current date and time of the system, and the result is shown in the form of long-date and long-time formats.

PowerShell Get-Date Format

If we use a Format-List cmdlet with Get-Date, we will see the full information that returns various dates and time formats. The command Get-Date returns a DateTime object. Let see the output of using the Get-Date | Format-List cmdlet:


PowerShell Get-Date Format

DisplayHint

The DisplayHint parameter defines the type of information we want to display on the screen. This parameter also returns the DateTime object; however, it has three different types of formats, including Date, Time, and DateTime. The -DisplayHint parameter uses anyone from Date, Time, or DateTime at a time.

  • Date: it only returns the current date value.
  • Time: it only returns a current time value.
  • DateTime: it returns both the current date and time values.

Let see the output of using the followings cmdlets:


PowerShell Get-Date Format

Format

Using the -Format parameter in the PowerShell command, you can display date and time in a specific format that you specify. The -Format parameter accepts all values which are allowed in the Microsoft .Net framework and included in the DateTimeFormatInfo Class. There are different format options available that you can use to format your date and time results according to your requirement.

Note that the result returned by -Format parameter is not the DateTime object; instead, they will a String result. We are mentioned some of the standard format specifiers information and their short description, which are provided by Microsoft.

Standard Strings Format Specifiers

Some of the standard .NET strings format specifiers that are mostly used are defined below:

SpecifierDescription
ddddreturns a day of the week in full name
MMreturns month number
ddreturns a day of the month in 2 digits
yyyyreturns a year in 4-digit
HH:mmreturns a time in 24-hour format without seconds
Kreturns the time zone offset from Universal Time Coordinate (UTC)

For example:

Let see an example to retrieve the date and time in a .NET format specifier using the following - Format parameter and several .NET format specifiers:


PowerShell Get-Date Format

Optional Format list

Date
dreturns short date pattern
Dreturns long date pattern
freturns full date/time pattern (short time)
Freturns full date/time pattern (long time)
greturns general date/time (short time)
Greturns general date/time (long time)
m or Mreturns month day pattern
oreturns round-trip date/time pattern
r or Rreturns RFC1123 pattern
sreturns sortable date/time pattern; conforms to ISO 8601
u or Ureturns sortable date/time pattern
y or Yreturns year month pattern
Time
treturns short time pattern
Treturns long time pattern

Let's apply be following format one by one and see their output:


PowerShell Get-Date Format

UFormat

The UFormat parameters allow us to mention a format that we want to display in Unix format. The syntax of this format command is as follows Get-Date -UFormat %\<value\>. The cmdlet -UFormat always returns an output as a String value. Below are various specifiers list and their descriptions that are used with -UFormat parameters provided by Microsoft.

List of UFormat Specifiers

Date and TimecReturns date and time in abbreviation (Wed Jan 6 18:35:23 2021)
TimepAM or PM
rreturns time in 12-hour format (08:24:43 AM)
Rreturns time in 24-hour format without seconds (15:32)
T or Xreturns time in 24-hour format (15:32:43)
Zreturns time zone offset from Universal Time Coordinate (UTC)
Hourk or Hreturns hour in 24-hour format (17)
I or l (upper case 'I', (lower case L)returns hour in 12-hour format (05)
Minutes & SecondsMreturns minutes (40)
Sreturns seconds (04)
sreturns seconds elapsed since January 1, 1970, 00:00:00 (1150451174.95705)
DateDreturns date in mm/dd/yy format (eg. 01/18/21)
xreturns date in standard format for locale (06/12/21 for English-US)
Monthb or hreturns month name in abbreviated (Jan)
Breturns month name in full (January)
mreturns month number (06)
YearCreturns century (21 for 2021)
g or yreturns year in 2-digit format (21)
G or Yreturns year in 4-digit format (2021)
WeekU or Wreturns the week of the year (00-52)
Vreturns the week of the year (01-53)
Dayareturns day of the week as abbreviated name (Mon)
Areturns day of the week as full name (Monday)
u or wreturns day of the week as number (Monday = 1)
dreturns day of the month in 2 digits (05)
ereturns day of the month - digit preceded by a space ( 5)
jreturns day of the year - (1-366)
Special Charactersnreturns newline character (\n)
treturns a tab character (\t)

Get-Date with -UFormat parameter that uses various format specifiers returns a String output. For example, look at the result of the following -UFormat cmdlet:


PowerShell Get-Date Format

How the above UFormat specifiers return the string value is defined below:

SpecifierDefinition
%Areturns day of the week as full name (Wednesday)
%mreturns month number (01)
%dreturns day of the month in 2 digits (06)
%Yreturns year in 4-digit format (2021)
%Rreturns time in 24-hour format without seconds (21:38)
%Zreturns time zone offset from Universal Time Coordinate (UTC) (+05).

Calculate and display a date's day of the year (Get-Date -Year 2021 -Month 12 -Day 31).DayOfYear

To calculate the date's of the year, the Get-Date uses three parameters to determine the date. These parameters are -Year, -Month, and -Day. Its command is enclosed within the parentheses so that the DayofYear property evaluates the output result. For example, calculating the total day at the given date of a year.


PowerShell Get-Date Format

Checking whether a date is adjusted for daylight savings time:

To check whether a date is adjusted for daylight savings time or not, it requires a boolean method. For example:


PowerShell Get-Date Format

In the above command, we use a variable $DST that stores the result of Get-Date. A variable $DST calls the IsDaylightSavingTime() method to test whether a date is adjusted for daylight savings time or not.

Convert the current time to UTC:

The ToUniversalTime() method is used to convert the local system's current time into a Universal Time Coordinate (UTC). Let's see an example to convert current time to UTC offset. First of all, we will get the current date and time with UTC; after that, we convert it to Universal Time.


PowerShell Get-Date Format

The Get-Date command uses a -UFormat parameter and some format specifiers to return the local system's current date and time. The format specifier %Z specifies the Universal Time Coordinate offset of +05.

A variable $Time store the current date and time result returned by Get-Date. The returned value ($Time) calls the ToUniversalTime() method to convert the local system's current date and time to UTC offset.

Create a timestamp using Get-Date -Format:

To create a timestamp using the Get-Date -Format, let's see the following cmdlet example:


PowerShell Get-Date Format

In the above example, the format specifier creates a timestamp String object for an input directory name. This timestamp contains values of date, time, and UTC offset.

In the above command, a variable $timestamp stores the results of the Get-Date command. Get-Date uses the Format parameter and a format specifier o that creates a timestamp String object in lowercase.

The object is sent back through the pipeline to ForEach-Object, and variable $_ represents the current pipeline object. The timestamp string values are specified by colons, which are replaced by periods.

The New-Item uses a -Path parameter that determines the location for the newly-created directory. A variable $timestamp included in the path represents the directory name, and the -Time parameter indicates that a directory is created.

PowerShell Get-Date Format
Next Topic#