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:

  1. Get-Date -UFormat “%A %m/%d/%Y %R %Z  
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.

  1. (Get-Date -Year 2021 -Month 12 -Day 31).DayOfYear  
  2. (Get-Date -Year 2021 -Month 06 -Day 31).DayOfYear  
  3. (Get-Date -Year 2021 -Month 02 -Day 31).DayOfYear  
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:

  1. $DST = Get-Date  
  2. $DST.IsDaylightSavingTime()  
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.

  1. Get-Date -UFormat “%A %B/%d/%Y %T %Z”  
  2. $Time = Get-Date  
  3. $Time.ToUniversalTime()  
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:

  1. $timestamp = Get-Date -Format o | ForEach-Object { $_ -replace “:”, “.” }  
  2. New-Item -Path D:\TestFolder\$timestamp -Type Directory  
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


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *