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 :

About these ads
This entry was posted in Powershell. Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s