Get-Date

Get the current date and time.

Syntax
      Get-Date [[-date] DateTime]
         [-displayHint {Date | Time | DateTime}]
           {[-format string] | [-uFormat string]}
              [-year int] [-month int] [-day int] [-hour int]
                 [-minute int] [-second int] [CommonParameters]

key
   -date DateTime
       By default, Get-Date returns the current system date and time.
       The -date parameter allows you to specify
       (usually via the pipeline) a specific date and time. 

   -displayHint DisplayHintType
       Display only the Date, only the Time or the DateTime.
       This does not affect the DateTime object that is retrieved.

   -format string
       Display the date and time in the .NET format
       as indicated by String representing a format specifier. 
        
   -uFormat string
       Display the date and time in Unix format.
       There is a bug in that %V fails to return am ISO Week No. see below for workarounds.
       The options %g, %G, %h, %k, %l, %N, %u,%U, %V, %w,%W, %X and %Z are also non-standard.

-year -month -day -hour -minute -second These allow you to set individual items to be displayed in place of the current date/time. e.g. you could set the time to 12:00

When you use -format or -uformat, PowerShell will retrieve only the properties that it needs to display the date in the format that you specify. As a result, some properties and methods of DateTime objects might not be available.

Date Properties:

$day = (get-date).day
$dayofweek = (get-date).dayofweek
$dayofyear = (get-date).dayofyear
$hour = (get-date).hour
$ms = (get-date).millisecond
$minute = (get-date).minute
$month = (get-date).month
$second = (get-date).second
$time = (get-date).timeofday
$year = (get-date).year

To see all the properties and methods of the DateTime object, type get-date | get-member

If you specify a value that is greater than the number of days in the month, PowerShell adds the number of days to the month and displays the result. For example, get-date -month 2 -day 31 will display "March 3", not "February 31".

Week of the Year

Although get-date -uformat is documented to return 'Unix format' and in Unix %V would return the ISO week number, Get-Date -uformat '%V' actually returns a 'Microsoft week' of the year. Week 1 is simply defined as the first 6 days of the year and subsequent weeks numbered every 7 days.

Why 6 days in the first week and not 7, I have no idea - it means that any weekly reporting done using these week numbers will have a 15%-20% error in the first week of the year.

Microsoft week vs Unix Week

ISO Standard Week Numbers always start on a Monday, and the first week of the year is always 7 days.

Week 1 of the year is the first week with at least four days from that year, so the first Thursday in the year is always in week 1.

Every ISO week is 7 days long but sometimes those weeks will span into a new year, so in the Jan 2027 example above the first 3 days will be in ISO week 53 of the previous year.

In Excel the function =ISOWEEKNUM(Date) will return a correct ISO week number.

Calculating a true ISO week number in PowerShell is a little more complex but here is a short script to do it:

$checkdate = Get-Date -date "2007-12-31"
$dow=[int]($checkdate).dayofweek
# if the day of week is before Thurs (Mon-Wed) add 3 since Thursday is the critical
# day for determining when the ISO week starts. Source[x]
if ($dow -match "[1-3]") {$checkdate.addDays(3)}
# Return the ISO week number
$(Get-Culture).Calendar.GetWeekOfYear(($checkdate),[System.Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)

# The ISOWeek.GetWeekOfYear Method will make this easier in .NET Core 3.0/ .Net standard 2.1

Examples

Retrieve the current date and time, but display only the date:

PS C:\> get-date -DisplayHint date

Retrieve the current date and time and store in the variable $start:

PS C:\> $start = Get-Date -format "dd-MMM-yyyy HH:mm"

Get the current time with Hours, Minutes and Seconds:

PS C:\> $time_now = Get-Date -format "HH:mm:ss"
14:43:04

Retrieve the current date and time in strict ISO 8601 format:

PS C:\> Get-Date -format s
2018-11-26T14:43:04

Get the current date and time with fractions of a second, in a format suitable to be used as a Windows filename:

PS C:\> get-date -format yyyy-MM-ddTHH-mm-ss-ff
2018-11-26T14-45-02-33

Retrieve the current date and time, display as a General short date/time:

PS C:\> get-date -format g

Display the day of the year:

PS C:\> (get-date).dayofyear

Get the day of the week as an integer (0=Sunday, 6=Saturday):

PS C:\> [Int]$dow = Get-Date | Select-Object -ExpandProperty DayOfWeek
PS C:\> $dow

Display yesterdays date, using the .AddDays method:

PS C:\> (Get-Date).AddDays(-1)

Get a specific date:

PS C:\> $mydate = Get-Date -date "2021-02-28"
or
PS C:\> "2021-02-28" | Get-Date

Display daylight savings and UTC:

PS C:\> $a = get-date
$a.IsDaylightSavingTime()
$a.ToUniversalTime()

# or display in ISO 8601 format:
$a.ToUniversalTime().ToString('yyyy-MM-dd HH:mm:ss')

Display the bios date of a remote machine using WMI:

PS C:\> $a = get-ciminstance win32_bios -computer SERVER64
$a | format-list -property Name, @{Label="BIOS Date "; `
Expression={$_.ConvertToDateTime($_.ReleaseDate)}}

The backtick character (`) is the line continuation character

“Carpe Diem - Seize the day” ~ Horace

Related PowerShell Cmdlets:

Set-Date - Set system time on the host system.
New-Timespan - Create a timespan object.
Rename-Item - Rename items to include the date or time.
Equivalent bash command: date - Display or change the date.


 
Copyright © 1999-2020 SS64.com
Some rights reserved