Start and End of week

Couple of ideas for finding the beginning and end of the week

Start of week

$s = get-date -hour 0 -minute 0 -second 0
$s.AddDays(-($s).DayOfWeek.value__)

End of week

$e = get-date -hour 23 -minute 59 -second 59
$e.AddDays(7-($s).DayOfWeek.value__)

To find the start of the week – use get-date and set the time portion to zero i.e. midnight

Then use the AddDays() method to subtract the value of the current day of the week.  Value__ is a property of a property – get-member to the rescue again!!  This gives the start of the week.

End of the week is similar except set the time to 1 second to midnight and use AddDays() to add the number of days left in the week i.e. seven minus the value of the current day of the week.

 

Share this post :

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

2 Responses to Start and End of week

  1. Bryce says:

    I know this is quite the old post but it is perfect information for what I am working on in a project. I do note that the AddDays functions end up displaying the Time information as well as the Date. Is there any easy way to only have it show the date itself, or even alternative formats for the date?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s