What's this?

You are currently reading Find The Date Of A Weekday at thirstycrow.

meta

Find The Date Of A Weekday

April 14th, 2011 Comments Off

I’m doing a quick one-off scheduling app for work and needed to present in the user interface the dates of the current and next three weeks’ Monday, Wednesday, and Friday. The DayOfWeek enumerator’s corresponding int values come in handy. The snippet below uses the int value of the DayOfWeek value from DateTime.Now to create an offset which is then subtracted from DateTime.Now to find the date of Sunday of the current week.

DateTime now = DateTime.Now;
int dayOfWeekOffset = (int)now.DayOfWeek;
DateTime firstDayOfWeek = now.AddDays(-dayOfWeekOffset);

From there you can add days to firstDayOfWeek to find the date of the relevant week day. For example:

//this week's Wednesday
DateTime wednesday = firstDayOfWeek.AddDays(3);
//next week's Friday
DateTime friday = firstDayOfWeek.AddDays(12);

Comments are closed.