PowerShell – exploring PowerShell CMDLET – getting date with options : Get-Date

Hi All,
Greetings for the day!!!
Today bit different article from Microsoft 365 / Azure / SharePoint but important one.
Background
Recently I got a chance to implement the PowerShell script in which I need to do some date-time calculations. So explored the Get-Date PowerShell CMDLET and hence the article.
In this article we will in depth discuss the Get-Date PowerShell CMDLET
print current date
PS C:> date
Wednesday, June 22, 2022 11:08:20 AM
Get-Date
- Details
- The
Get-Date
CMDLET gets a DateTime object that represents the current date or a date that we specify Get-Date
CMDLET can format the date and time in several .NET and UNIX formats- Get-Date uses the computer’s culture settings to format the output date
- We can use “Get-Culture” CMDLET to know the our computers date time settings
- The
PS C:\> (Get-Culture).DateTimeFormat
AMDesignator : AM
Calendar : System.Globalization.GregorianCalendar
DateSeparator : /
FirstDayOfWeek : Sunday
CalendarWeekRule : FirstDay
FullDateTimePattern : dddd, MMMM d, yyyy h:mm:ss tt
LongDatePattern : dddd, MMMM d, yyyy
LongTimePattern : h:mm:ss tt
MonthDayPattern : MMMM d
PMDesignator : PM
RFC1123Pattern : ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
ShortDatePattern : M/d/yyyy
ShortTimePattern : h:mm tt
SortableDateTimePattern : yyyy'-'MM'-'dd'T'HH':'mm':'ss
TimeSeparator : :
UniversalSortableDateTimePattern : yyyy'-'MM'-'dd HH':'mm':'ss'Z'
YearMonthPattern : MMMM yyyy
AbbreviatedDayNames : {Sun, Mon, Tue, Wed...}
ShortestDayNames : {Su, Mo, Tu, We...}
DayNames : {Sunday, Monday, Tuesday, Wednesday...}
AbbreviatedMonthNames : {Jan, Feb, Mar, Apr...}
MonthNames : {January, February, March, April...}
IsReadOnly : False
NativeCalendarName : Gregorian Calendar
AbbreviatedMonthGenitiveNames : {Jan, Feb, Mar, Apr...}
MonthGenitiveNames : {January, February, March, April...}

- Syntax
Get-Date
[[-Date] <DateTime>]
[-Year <Int32>]
[-Month <Int32>]
[-Day <Int32>]
[-Hour <Int32>]
[-Minute <Int32>]
[-Second <Int32>]
[-Millisecond <Int32>]
[-DisplayHint <DisplayHintType>]
[-Format <String>]
[-AsUTC]
[<CommonParameters>]
Get-Date
[[-Date] <DateTime>]
[-Year <Int32>]
[-Month <Int32>]
[-Day <Int32>]
[-Hour <Int32>]
[-Minute <Int32>]
[-Second <Int32>]
[-Millisecond <Int32>]
[-DisplayHint <DisplayHintType>]
-UFormat <String>
[<CommonParameters>]
Get-Date
-UnixTimeSeconds <Int64>
[-Year <Int32>]
[-Month <Int32>]
[-Day <Int32>]
[-Hour <Int32>]
[-Minute <Int32>]
[-Second <Int32>]
[-Millisecond <Int32>]
[-DisplayHint <DisplayHintType>]
[-Format <String>]
[-AsUTC]
[<CommonParameters>]
Get-Date
-UnixTimeSeconds <Int64>
[-Year <Int32>]
[-Month <Int32>]
[-Day <Int32>]
[-Hour <Int32>]
[-Minute <Int32>]
[-Second <Int32>]
[-Millisecond <Int32>]
[-DisplayHint <DisplayHintType>]
-UFormat <String>
[<CommonParameters>]
- Examples
Get the current date and time
PS C:\> get-date
Wednesday, June 22, 2022 11:10:58 AM
Get the date properties using Format-List or fl
PS C:\> Get-Date | Format-List
DisplayHint : DateTime
Date : 12/7/2022 12:00:00 AM
Day : 7
DayOfWeek : Wednesday
DayOfYear : 341
Hour : 5
Kind : Local
Millisecond : 790
Minute : 8
Month : 12
Second : 21
Ticks : 638059865017901748
TimeOfDay : 05:08:21.7901748
Year : 2022
DateTime : Wednesday, December 7, 2022 5:08:21 AM
Format date with specific string
PS C:\> get-date -format "M-d-yyyy"
6-22-2022
PS C:\> get-date -format "M-d-yyyy-hh:mm:ss"
6-22-2022-11:13:50
Get the short date with “-Format g” parameter
PS C:\> Get-Date -Format g
12/7/2022 5:13 AM
Get only current time with “-Format t” parameter
PS C:\> Get-Date -Format t
5:15 AM
Get short date with parameter – “-Format d”
PS C:\> Get-Date -Format d
12/7/2022
Getting only elements of current date and time using – DisplayHint attribute
PS C:\> Get-Date -DisplayHint Date
Wednesday, December 7, 2022
PS C:\> Get-Date -DisplayHint DateTime
Wednesday, December 7, 2022 4:47:09 AM
PS C:\> Get-Date -DisplayHint Time
4:47:20 AM
Get a date’s day of the year – Can be used to identify the leap year
PS C:\> (Get-Date -Year 2022 -Month 12 -Day 31).DayOfYear
365
PS C:\> (Get-Date -Year 2020 -Month 12 -Day 31).DayOfYear
366
Check if a date is adjusted for daylight savings time – using IsDaylightSavingTime()
PS C:\> $date = Get-Date
PS C:\> $date.IsDaylightSavingTime()
False

To display only specific part – Day, Month or Year
PS C:\> $date = Get-Date
PS C:\> $date.Day
7
PS C:\> $date.Month
12
PS C:\> $date.Year
2022
Thanks for reading the article !!! Please feel free to discuss in case any issues / suggestions / thoughts / questions !!!
HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂
You must log in to post a comment.