Javatpoint Logo
Javatpoint Logo

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:

Specifier Description
dddd returns a day of the week in full name
MM returns month number
dd returns a day of the month in 2 digits
yyyy returns a year in 4-digit
HH:mm returns a time in 24-hour format without seconds
K returns 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
d returns short date pattern
D returns long date pattern
f returns full date/time pattern (short time)
F returns full date/time pattern (long time)
g returns general date/time (short time)
G returns general date/time (long time)
m or M returns month day pattern
o returns round-trip date/time pattern
r or R returns RFC1123 pattern
s returns sortable date/time pattern; conforms to ISO 8601
u or U returns sortable date/time pattern
y or Y returns year month pattern
Time
t returns short time pattern
T returns 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 Time c Returns date and time in abbreviation (Wed Jan 6 18:35:23 2021)
Time p AM or PM
r returns time in 12-hour format (08:24:43 AM)
R returns time in 24-hour format without seconds (15:32)
T or X returns time in 24-hour format (15:32:43)
Z returns time zone offset from Universal Time Coordinate (UTC)
Hour k or H returns hour in 24-hour format (17)
I or l (upper case 'I', (lower case L) returns hour in 12-hour format (05)
Minutes & Seconds M returns minutes (40)
S returns seconds (04)
s returns seconds elapsed since January 1, 1970, 00:00:00 (1150451174.95705)
Date D returns date in mm/dd/yy format (eg. 01/18/21)
x returns date in standard format for locale (06/12/21 for English-US)
Month b or h returns month name in abbreviated (Jan)
B returns month name in full (January)
m returns month number (06)
Year C returns century (21 for 2021)
g or y returns year in 2-digit format (21)
G or Y returns year in 4-digit format (2021)
Week U or W returns the week of the year (00-52)
V returns the week of the year (01-53)
Day a returns day of the week as abbreviated name (Mon)
A returns day of the week as full name (Monday)
u or w returns day of the week as number (Monday = 1)
d returns day of the month in 2 digits (05)
e returns day of the month - digit preceded by a space ( 5)
j returns day of the year - (1-366)
Special Characters n returns newline character (\n)
t returns 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:

Specifier Definition
%A returns day of the week as full name (Wednesday)
%m returns month number (01)
%d returns day of the month in 2 digits (06)
%Y returns year in 4-digit format (2021)
%R returns time in 24-hour format without seconds (21:38)
%Z returns 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#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA