...let John help you code



How to set Monday as Start Date of the Week in C#

Asked by:
qAsker
Dec 11, 2016, 15:03

Description

I have a Date field getting from User. I want to show the start date of the week for the date coming from the user.

1 Answer

Answer :
Code

Preview
 

8

Let's say you have the following situation:
User provided date = userDate
You want to display the week start date for the userDate .
Here is how you can achieve this:

When you want your start day of the week as Sunday :


var sundayWeekStartDay = userDate.AddDays(-(int)userDate.DayOfWeek);  

When you want any other day as start day of the week (eg: Monday )


var mondayWeekStartDay = userDate.AddDays(-(int)userDate.DayOfWeek + (int)DayOfWeek.Monday);  

Frank-en-teins