Find the date on Sunday

RIAS
RIAS used Ask the Experts™
on
Hello,
Given a date, how to get the date on Sunday for that week.
One line code will be great!

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Like this:
Imports System.Runtime.CompilerServices

Module Module1
	Sub Main()
		Dim d1 = New Date(2019, 9, 25)

		Console.WriteLine($"Sunday: {d1.GetDate(DayOfWeek.Sunday).ToLongDateString()}")
		Console.WriteLine($"Monday: {d1.GetDate(DayOfWeek.Monday).ToLongDateString()}")

		Console.ReadLine()
	End Sub
End Module

Module Extensions
	<Extension>
	Public Function GetDate(ByVal [date] As Date, ByVal day As DayOfWeek) As Date
		GetDate = [date].AddDays(-CInt([date].AddDays(-CInt(day)).DayOfWeek))
	End Function
End Module

Open in new window

Which produces the following output -Capture.PNG-saige-
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I guess my first question would be do you want the Sunday before the date, or the Sunday after?


»bp

Author

Commented:
Hello,
Thanks Bill,
Sunday of that current week.

Author

Commented:
Thank you Sir!!!

Commented:
Glad to be of assistance.

-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