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

PowerShell - our computers date time settings
PowerShell - our computers date time settings

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

fig : PowerShell - printing current date
fig : PowerShell – printing current date

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

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...}

fig : PowerShell - our computers date time settings
fig : PowerShell – our computers date time settings
  • 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 current date and time
fig : PowerShell – Get the current date and time

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

Get the date properties using Format-List or fl
fig : PowerShell – Get the date properties using Format-List or fl

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

Format date with specific string
fig : PowerShell – Format date with specific string

Get the short date with “-Format g” parameter

PS C:\> Get-Date -Format g
12/7/2022 5:13 AM

Get the short date with "-Format g" parameter
fig : PowerShell – Get the short date with “-Format g” parameter

Get only current time with “-Format t” parameter

PS C:\> Get-Date -Format t
5:15 AM

Get only current time with "-Format t" parameter
fig : PowerShell – Get only current time with “-Format t” parameter

Get short date with parameter – “-Format d”

PS C:\> Get-Date -Format d
12/7/2022

Get short date with parameter - "-Format d"
fig : PowerShell – Get short date with parameter – “-Format d”

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

Getting only elements of current date and time using - DisplayHint attribute
fig : PowerShell – Getting only elements of current date and time using – DisplayHint attribute

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

Get a date's day of the year - Can be used to identify the leap year
fig : PowerShell – Get a date’s day of the year – Can be used to identify the leap year

Check if a date is adjusted for daylight savings time – using IsDaylightSavingTime()

PS C:\> $date = Get-Date

PS C:\> $date.IsDaylightSavingTime()
False

Check if a date is adjusted for daylight savings time - using IsDaylightSavingTime()
fig : PowerShell – Check if a date is adjusted for daylight savings time – using IsDaylightSavingTime()

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

fig : PowerShell - To display only specific part - Day, Month or Year
fig : PowerShell – To display only specific part – Day, Month or Year

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 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.