I am trying to get the start of the week from a given date. For example if i passed in a DateTime of 16/08/2018 i would expect a return of 13/08/2018. Everything i have found is to find the start of the current week.
Asked
Active
Viewed 5,118 times
-1
-
Possible duplicate of [Get all the dates of current week](https://stackoverflow.com/questions/43122759/get-all-the-dates-of-current-week) Replace the current Date with whatever date you want. – Chetan Aug 20 '18 at 10:04
-
3Once you have a `DateTime` then you have its `DayOfWeek` property. Once you have that then you can determine how many days prior is the "first" day of that week. What have you tried? – David Aug 20 '18 at 10:05
-
Using `DateTime.DayOfWeek` would give you the information you need to make the calculation. (see [this question](https://stackoverflow.com/questions/9199080/how-to-get-the-integer-value-of-day-of-week) for details on using `DayOfWeek`) – Diado Aug 20 '18 at 10:06
-
Possible duplicate of [How can I get the DateTime for the start of the week?](https://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week) – mjwills Aug 20 '18 at 10:28
4 Answers
1
I would do something like this:
Parse the date from string to DateTime object (if required)
Add days current day of the week * -1 (turn it negative) + 1
string s = "2018-08-23 13:26";
DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
DateTime startOfWeek = dt.AddDays(((int)(dt.DayOfWeek) * -1) + 1);
Console.WriteLine(startOfWeek);
Edit: If you are bothered about the sunday going to the next Monday, then change the Sunday to remove 7 days from the current date like so;
string s = "2018-08-17 13:26";
DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
bool isSunday = dt.DayOfWeek == 0;
var dayOfweek = isSunday == false ? (int)dt.DayOfWeek : 7;
DateTime startOfWeek = dt.AddDays(((int)(dayOfweek) * -1) + 1);
Console.WriteLine(startOfWeek);

Adrian
- 8,271
- 2
- 26
- 43
-
1
-
i would be passing in dates from monday to friday (working days). So returning the following monday from the sunday wont affect me. – Stuart Bradley Aug 20 '18 at 10:21
1
Here's a version gives you the monday of the week (assumes that weeks start on monday and end on sunday)
DateTime dt = DateTime.Now;
DateTime startOfWeek = dt.AddDays((((int)(dt.DayOfWeek) + 6) % 7) * -1);
Console.WriteLine(startOfWeek);

Hans Kilian
- 18,948
- 1
- 26
- 35
1
Get Monday and Sunday of the week with date variable. Can easy try with date variable.
DateTime date = new DateTime(2019, 12, 1);
int day = (int) date.DayOfWeek;
DateTime Monday = date.AddDays((-1) * (day == 0 ? 6 : day - 1));
DateTime Sunday = date.AddDays((1) * (day== 0 ? day : 7 - day)); ```

Ralentix
- 91
- 1
- 6
-
2Please add some explanation to improve your answer, and so others can understand *how* this fixes the problem. – Kevin Jan 09 '20 at 13:15
0
Super Simple! You can use the DayOfWeek to get how far back you have to go. More info Is avaliable here
Sundays are 0, Mondays are 1.
DateTime date = DateTime.Now; //gets current time
int day = date.DayOfWeek; //gets current day
DateTime Sunday = date.AddDays( (-1) * day ); //go back all the days to get to Sunday
DateTime Monday = date.AddDays( (-1) * (day - 1) ); //go back all the days - 1 to get to Monday
As pointed out by a comment this doesn't work if the current day is on a sunday... So the Monday fix is:
DateTime Monday = date.AddDays( (-1) * (day == 0 ? 7 : day - 1) ); //go back all the days - 1 to get to Monday

DaMachk
- 643
- 4
- 10
-
@mjwills the previous monday, we are "adding" a negative number there, hence we are going back on the timeline. – DaMachk Aug 20 '18 at 10:49
-
Yeah... the edge case of where the current day is on a sunday makes my approach add 1 day instead of substracting 6. Nothing a simple ``day == 0 ? 7 : day`` wouldn't solve :D – DaMachk Aug 20 '18 at 10:59
-
Thank you for pointing it out! Guess i should have taken more than a minute to think of the reply :D – DaMachk Aug 20 '18 at 11:02