Reputation: 11188
Using a Powershell date object how do you get the day number of the week, 0 to 6 where 0 would be Sunday and 6 would be Saturday. I know that I can get the day name with the code below but how do I get the number as there is no DayNumberOfWeek or equivalent property?
(Get-Date).DayOfWeek
I suppose I could use the day name from the code above in a switch statement to convert it to a number but that doesn't seem to be very eloquent.
Upvotes: 19
Views: 87554
Reputation: 1702
On Friday the 17th:
(get-date).DayOfWeek.value__
Returns 5
(Get-Date).DayOfWeek
Returns Friday
(Get-Date).Day
Returns 17
Upvotes: 2
Reputation: 24535
Well, the DayOfWeek
property of a DateTime
is not a string but rather a DayOfWeek
enum, so the shortest answer is probably
[Int] (Get-Date).DayOfWeek # returns 0 through 6 for current day of week
Or
[Int] [DayOfWeek] "Wednesday" # returns 3
Upvotes: 30
Reputation: 189
Get-Date -UFormat %u
will return formated date.
check http://technet.microsoft.com/en-us/library/hh849887.aspx for more fomats
Upvotes: 6
Reputation: 60908
like this:
( get-date ).DayOfWeek.value__
I suggest for the future to investigate what properties an object in this way:
( get-date ).DayOfWeek | gm -f # gm is an alias for get-member
Upvotes: 33