DatePart


Definition: Returns the specified part of a given date.

 

Given a date-time value the DatePart function can tease out just a portion of that value, such as the hour, minute, or second of the day. In Windows PowerShell the Get-Date Cmdlet provides the same capability: just call Get-Date and take a peek at the desired property value. Here are some examples:

 

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

 

Suppose you do need to know just the hour of the day. No problem; first, use this command to grab the value of the Hour property and store it in the variable $a:

 

$a = (get-date).hour

 

And then simply echo back the value of $a. Depending on the time of day when you ran the command, you should get back something like this (based on a 24-hour clock):

 

15