vb.net Monthcalendar dates

FCapo
FCapo used Ask the Experts™
on
hi,

Is there a way to get the start and end date of the current week?

For example, today is the 7/31/2015, I'd like to get the start date of sunday (July 26), and then the end date of saturday (August 1st) from the calendar.

Basically the week that includes todays date.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Distinguished Expert 2017
Commented:
Hi FCapo;

This should give you what you need.

Dim dateNow As DateTime = DateTime.Now
'' Get the current day of the week 0 - 6, 0 = Sunday and 6 = Saturday
Dim currentDay As Integer = dateNow.DayOfWeek
'' Get start of the week by subtracting current day of the week
Dim startOfWeek As DateTime = DateTime.Now.AddDays(-currentDay)
'' Get end of the week by adding 6 to start
Dim endOfWeek As DateTime = startOfWeek.AddDays(6)

Open in new window

Commented:
*No Points*

Proof of concept using Fernando's recommendation:
Module Module1
	Sub Main()
		Dim now = DateTime.Now
		' Get the current day of the week 0 - 6, 0 = Sunday and 6 = Saturday
		Dim current = now.DayOfWeek
		' Get the start of the week by subtracting current day of week
		Dim start = New DateTime(now.Year, now.Month, now.Day).AddDays(-current)
		' Get the end of the week by adding 6 to the start.
		Dim [end] = start.AddDays(6)

		Console.WriteLine("The first date of the week for {0} is {1}", now.ToShortDateString(), start.ToShortDateString())
		Console.WriteLine("The last date of the week for {0} is {1}", now.ToShortDateString(), [end].ToShortDateString())
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial