PowerShell Get-Date Command Tutorial

Windows PowerShell provides the Get-Date command in order to get the current date and time information. Get-Date can provide the current date and time information in different formats like .NET or UNIX. Get-Data command can be used in PowerShell and do not support MS-DOS. Also, the output of the Get-Data command can be redirected into other commands or cmdlets too. The PowerShell commands are also called cmdlets so the Get-Date command can be also called Get-Date cmdlet too.

Get-Date Help Information

Get-Date command help information can be printed by using the Get-Help command. The help information provides the options offered by the Get-Date command.

PS> Get-Help Get-Date
Get-Date Help Information

We can see that the help information of the Get-Date command provides the syntax, aliases, and remarks.

Display Date and Time

By using the Get-Date command without any parameter or options will display the current date and time completely. The display format will be set according to the Windows regional settings.

PS> Get-Date

From the output, we can see that the current operating system language is English. The current day is Sunday, the Month is September, Day of the month is 20, Year is 2020, Hour is 08 AM, the Minute is 11, and the second is 27.

Get Only Date

The Get-Date command provides the -DisplayHint option where only date or time information can be displayed. In order to display only date information Date parameter can be used like below.

PS> Get-Date -DisplayHint Date

The output will be like below which contains the current weekday name, Current month, current day of the month, and the current year.

Tuesday, November 17, 2020

Get Only Time

Only time information can be displayed using the DisplayHint option with the Time parameter like below.

PS> Get-Date -DisplayHint Time

The current time output will be like below. This output contains the hour, minute and second information with AM or PM.

4:40:05 PM

Get Date and Time In Specified Format

Date and Time can be expressed or displayed in different formats which are related to the current regional and language settings of the operating system. You can use the -format option in order to specify a different format than the default. The following format specifiers are used to create and set format to display date and time.

READ  "ipconfig /renew" - Renew IP Address In Windows
SpecifierMeaning
ddDay of the month 2 digits
ddddDay of week full name
MMMonth number
yyyyYear in 4-digit format
yyYear in 2-digit format
KTimezone offset from UTC
hhHour in 12 hours AM/PM format
mmMinute
ssSecond

The following date and time specifiers are popularly used.

PS> Get-Date -format "dd-MMM-yyyy HH:mm"

PS> Get-Date -format "HH:mm:ss"

PS> Get-Date -format "HH:mm:ss"

PS> Get-Date -format "dd-MM-yy"

PS> Get-Date -format "dd-MM-yyyy"

Get Specific Part/Property Of The Date or Time

The only specific value of the given date can be retrieved with the Get-Date command. We will provide the part of the date like day, day of the week, year, etc. like below.

(Get-Date).day

(Get-Date).dayofweek

(Get-Date).dayofyear

(Get-Date).month

(Get-Date).year
Get Specific Part/Property Of The Date or Time

We can also get a specific value of the time. We will provide the property name like in the Date example. The Hour, Minute, Second, Ticks properties will be used for the following example.

  • Hour is the current hour in 24 hours format.
  • Minute is the current minute in 60 minutes format.
  • Second is the current second in 60 seconds format.
  • Ticks are the total seconds from 1970 as you can see it is a very big number.
PS> (Get-Date).Hour

PS> (Get-Date).Minute

PS> (Get-Date).Second

PS> (Get-Date).Ticks

The Get-Date command returns a date and time object which provides a lot of properties and date time-related information. We provided some of them here but all of them can be listed by using the following Get-Member cmdlet.

PS> Get-Date | Get-Member

Display Yesterday Date

Yesterday can be displayed by using the AddDays() methods which can be used to forward and backward to the specified day. We will get the current day with the Get-Day command and then remove 1 day from this date which will be yesterday. As AddDays() method adds a given day we will specify -1 to add which means remove specified days.

PS> (Get-Date).AddDays(-1)

PS> (Get-Date).AddDays(-7)
Display Yesterday Date

Display Tomorrow Date

By using the AddDays() method we can also forward to the future dates and times like tomorrow. We can display tomorrow’s date by adding 1 day into the current date.

PS> (Get-Date).AddDays(1)

PS> (Get-Date).AddDays(7)
Display Tomorrow Date

Get Day Of Year For Current or Specified Date

As we know a year consists of 365 days. We can use the DayOfYear property in order to get the day number of the specified day in a year.

PS> (Get-Date).DayOfYear

PS> (Get-Date -Year 2010 -Month 12 -Day 1).DayOfYear

Convert Current Time To UTC Time

UTC time is used to synchronize different time zones and set a single time for meetings or operations. UTC time can be converted using the ToUniversalTime() method.

PS> (Get-Date).ToUniversalTime()
Convert Current Time To UTC Time

Display Daylight Savings

Daylight saving is a method in order to change time according to the daylight in order makes work hours in the daylight. Generally, time is changed 0.5, 1.0, or 1.5 hours to make workhours in the daylight. Daylight saving is activated automatically and can be checked if it is currently active by using a date object IsDaylightSavingTime() method.

PS> (Get-Date).IsDaylightSavingTime()
Display Daylight Savings

Get Current DateTime Format

Current system date-time format can be displayed with the Get-Culture command DateTimeFormat property.

PS> (Get-Culture).DateTimeFormat
Get Current DateTime Format

Related PowerShell Commads/Cmdlets

Get-Date command or cmdlet is used to show current or specified date and time information. Get-Date is not used to set the current system date and time on a Windows operating system. Set-Date can be used to set the current system date and time.

READ  How To Install Windows 20 October 2020 Update (20H2) Version?

Leave a Comment