How Can I Determine the Date for the Friday in a Given Week?
Hey, Powershell! Given a date, can a script tell me the date that the Friday for that week occurs?
Here’s a script that – given a date – will report back the date that the Friday of that week falls on:
param ([system.datetime]$date = $([system.datetime]::now))
$intday = [int] $date.DayOfWeek
$intadder = 5 - $intday
$dateFriday = $date.AddDays( $intadder)
Write-Output "The friday of the given date is $dateFriday"
Let's see how this script works The script begins by assigning the date from commandline or current date to a variable named $date.
Next we use the DayofWeek property to determine the day of the week for given date DayofWeek is going to return one of the following value
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
This gives us a value representing the day of the week; What we need to do now is programmatically determine the number of days between this date and the Friday of that week. That’s something we can do with a single line of code:
$intadder = 5 - $intDay
Confused? Don’t be; this actually makes sense. If you refer to the table you’ll see that Friday has a value of 5. If we take the value for Friday (5) and subtract the value for given date we will get the days to add to get Friday. That’s what we do with this line of code:
$dateFriday = $date.AddDays($intddder)
System.DateTime is a powerful class that allows you to do date manipulation very easily in PowerShell
----------------------------------------------------------------------------------------------------------------------
Arul Kumaravel
Development Manager
Windows PowerShell
Comments
- Anonymous
June 16, 2009
PingBack from http://topalternativedating.info/story.php?id=3578