Get-Date

 

Additional Resources for Get-Date

 

Listing Date and Time Information

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/get-date.mspx

 

 

SYNOPSIS

Gets the current date and time.

 

SYNTAX

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

 

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

 

DETAILED DESCRIPTION

Gets the current date and time.

 

PARAMETERS

 

-date <DateTime>

Specifies a date and time. By default, Get-Date gets the current system date and time.

 

Required?

false

Position?

1

Default value

 

Accept pipeline input?  

true (ByValue, ByPropertyName)

Accept wildcard characters? 

false

 

-displayHint <DisplayHintType>

Determines which elements of the date and time are displayed. (This does not affect the DateTime object that Get-Date retrieves.)

 

Valid values are:

 

·          date: displays only the date

·          time: displays only the time

·          datetime: displays the date and time

 

The following lists the acceptable values for this parameter:

 

·          Date

·          Time

·          DateTime

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-format <string>

Displays the date and time in the .NET format indicated by the format specifier. <String> represents the format specifier. For a list of available format specifiers, see the System.Globalization.DateTimeFormatInfo Class topic in MSDN.

 

When you use -format, Windows PowerShell retrieves only the properties of the DateTime object 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.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-uFormat <string>

Displays the date and time in Unix format. For a list of the format specifiers, see Additional Notes.

 

When you use -uformat, Windows PowerShell retrieves only the properties of the DateTime object 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.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-year <int>

Specifies the year that is displayed. Enter a value from 1 - 9999. This value is displayed instead of the current year.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-month <int>

Specifies the month that is displayed. Enter a value from 1 - 12. This value is displayed instead of the current month.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-day <int>

Specifies the day of the month that is displayed. Enter a value from 1 - 31. This value is displayed instead of the current day.

 

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

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-hour <int>

Specifies the hour that is displayed. Enter a value from 1 - 23. This value is displayed instead of the current hour.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-minute <int>

Specifies the minute that is displayed. Enter a value from 1 - 59. This value is displayed instead of the current minute.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-second <int>

Specifies the second that is displayed. Enter a value from 1 - 59. This value is displayed instead of the current second.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

NOTES

 

For more information, type "Get-Help Get-Date -detailed". For technical information, type "Get-Help Get-Date -full".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

Uformat Values :

 

The following are the values of the -Uformat parameter. The format for the command is:

 

get-date -uformat %<value>

 

For example:

 

get-date -format %d

 

Date-Time:

·          Date and time - full 

·          (default) (Friday, June 16, 2006 10:31:27 AM) c Date and time - abbreviated (Fri Jun 16 10:31:27 2006)

 

Date:

·          DDate in mm/dd/yy format (06/14/06)

·          xDate in standard format for locale (09/12/07 for English-US)

 

Year:

·          C   Century (20 for 2006)

·          Y   Year in 4-digit format (2006)

·          y   Year in 2-digit format (06)

·          G   Same as 'Y'

·          g   Same as 'y'

 

Month:

·          b   Month name - abbreviated (Jan)

·          B   Month name - full (January)

·          h   Same as 'b'

·          M   Month number (06)

 

Week:

·          W  Week of the year (00-52)

·          V   Week of the year (01-53)

·          U   Same as 'W'

 

Day:

·          a   Day of the week - abbreviated name (Mon)

·          A   Day of the week - full name (Monday)

·          u   Day of the week - number (Monday = 1)

·          d   Day of the month - 2 digits (05)

·          e   Day of the month - digit preceded by a space ( 5)

·          jDay of the year - (1-366)

·          w   Same as 'u'

 

Time:

·          p   AM or PM

·          r   Time in 12-hour format (09:15:36 AM)

·          R   Time in 24-hour format - no seconds (17:45)

·          T   Time in 24 hour format (17:45:52)

·          X   Same as 'T'

·          Z   Time zone offset from UTC (-07)

 

Hour:

·          H   Hour in 24-hour format (17)

·          IHour in 12 hour format (05)

·          k   Same as 'H'

·          lSame as 'I' (Upper-case I = Lower-case L)

 

Minutes & Seconds:

·          m   Minutes (35)

·          S   Seconds (05)

·          s   Seconds elapsed since January 1, 1970 00:00:00 (1150451174.95705)

 

Special Characters:

·          n   newline character (\n)

·          t   Tab character (\t)

 

EXAMPLE 1

 

get-date -DisplayHint date

 

This command retrieves a DateTime object, but displays only the date. It uses the -DisplayHint parameter to indicate that only the date is to be displayed.

 

Tuesday, June 13, 2006

 

EXAMPLE 2

 

get-date -format g

 

This command retrieves the current date and time, and formats it in short date and short time format. It uses the .NET "g" format specifier (General (short date and short time)) to specify the format.

 

6/13/2006 12:43 PM

 

EXAMPLE 3

 

get-date -uformat "%Y / %m / %d / %A / %Z"

 

This command retrieves the current date and time, and formats it as specified by the command. In this case, the format includes the full year (%Y), the two-digit numeric month (%m), the date (%d), the full day of the week (%A), and the offset from UTC ("Zulu").

 

2006 / 06 / 13 / Tuesday / -07

 

EXAMPLE 4

 

(get-date -year 2000 -month 12 -day 31).dayofyear

 

This command displays the day of the year for the current date. For example, December 31 is the 365th day of 2006, but it's the 366th day of 2000.

 

366

 

EXAMPLE 5

 

$a = get-date

$a.IsDaylightSavingTime()

 

These commands tell you whether the current date and time are adjusted for daylight savings time in the current locale.

 

The first command creates a variable named $a and then assigns the object retrieved by Get-Date to the $a variable. Then it uses the IsDaylightSavingTime method on the object in $a.

 

To see the properties and methods of the DateTime object, type "get-date get-member".

 

True

 

EXAMPLE 6

 

$a = get-date

$a.ToUniversalTime()

 

These commands convert the current date and time to UTC time.

 

The first command creates a variable named $a and then assigns the object retrieved by Get-Date to the $a variable. Then it uses the ToUniversalTime method on the object in $a.

 

Tuesday, June 13, 2006 8:09:19 PM

 

EXAMPLE 7

 

$a = get-wmiobject win32_bios -computer server01

 

$a | format-list -property Name, @{Label="BIOS Age"; `

Expression={(get-date) - $_.ConvertToDateTime($_.ReleaseDate)}}

 

Windows Management Instrumentation (WMI) uses a different date-time object than the .NET date-time object that Get-Date returns. To use date-time information from WMI in a command with date-time information from Get-Date, you have to use the ConvertToDateTime method to convert WMI CIM_DATETIME objects to a .NET DateTime objects.

 

The commands in this example display the name and age of the BIOS on a remote computer, Server01. The first command uses the Get-WmiObjectcmdlet to get an instance of the Win32_BIOS class on Server01 and then stores it in the $a variable.

 

The second command uses the pipeline operator (|) to send the WMI object stored in $a to the Format-Listcmdlet. The -Property parameter of Format-List is used to specify two properties to display in the list, "Name" and "BIOS Age". The "BIOS Age" property is specified in a hash table. The table includes the Label key, which specifies the name of the property, and the Expression key which contains the expression that calculates the BIOS age. The expression uses the ConvertToDateTime method to convert each instance of ReleaseDate to a .NET DateTime object. Then, the value is subtracted from the value of the Get-Date cmdlet, which, without parameters, gets the current date.

 

The backtick character (`) is the line continuation character in Windows PowerShell.

 

Name : Default System BIOS

BIOS Age : 1345.17:31:07.1091047

 

RELATED LINKS

Set-Date

New-TimeSpan