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 does 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
We can see that the help information of the Get-Date command provides the syntax, aliases, and remarks. As we can see that the Get-Date command provides options related to year, month, day, hour, minute, etc.
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.
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.
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.
|dd||Day of the month 2 digits|
|dddd||Day of week full name|
|yyyy||Year in 4-digit format|
|yy||Year in 2-digit format|
|K||Timezone offset from UTC|
|hh||Hour in 12 hours AM/PM format|
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
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. Tick is a less known value about the date and time. Ticks are the total seconds from 1 January 1970.
- 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 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)
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
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
Get Current DateTime Format
Current system date-time format can be displayed with the
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.