How do I find the start of the week (both Sunday and Monday) knowing just the current time in C#?
Something like:
DateTime.Now.StartWeek(Monday);
How do I find the start of the week (both Sunday and Monday) knowing just the current time in C#?
Something like:
DateTime.Now.StartWeek(Monday);
This would give you midnight on the first Sunday of the week:
This gives you the first Monday at midnight:
Here is a combination of a few of the answers. It uses an extension method that allows the culture to be passed in, if one is not passed in, the current culture is used. This will give it max flexibility and re-use.
Example Usage:
Putting it all together, with Globalization and allowing for specifying the first day of the week as part of the call we have
Tried several but did not solve the issue with a week starting on a Monday, resulting in giving me the coming Monday on a Sunday. So I modified it a bit and got it working with this code:
Use an extension method. They're the answer to everything, you know! ;)
Which can be used as follows: