Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get range of days in a week given a certain day

Tags:

date

c#

Suppose my input is March 31, 2015.
How would I be able to get the days of the week that March 31, 2015 is in?
In this case, it should output:
March 29, 2015-April 4, 2015

I found something similar here but it's not quite what I'm looking for as that one takes in the week number while my input is a date itself.

like image 746
Katie Avatar asked Oct 16 '25 18:10

Katie


1 Answers

DateTime date = new DateTime(2015, 3, 31);
DateTime weekFirstDay = date.AddDays(DayOfWeek.Sunday - date.DayOfWeek);
DateTime weekLastDay = weekFirstDay.AddDays(6);
like image 140
Ulugbek Umirov Avatar answered Oct 18 '25 12:10

Ulugbek Umirov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!