<p>How do I find the start of the week (both Sunday and Monday) knowing just the current time in C#?</p>

<p>Something like:</p>

<pre><code>DateTime.Now.StartWeek(Monday);
</code></pre>

[{'answer_id': 38049, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 4, 'selected': False, 'text': '<p>This would give you the preceding Sunday (I think):</p>\n\n<pre><code>DateTime t = DateTime.Now;\nt -= new TimeSpan ((int) t.DayOfWeek, 0, 0, 0);\n</code></pre>\n'}, {'answer_id': 38051, 'author': 'firedfly', 'author_id': 3123, 'author_profile': 'https://Stackoverflow.com/users/3123', 'pm_score': 1, 'selected': False, 'text': '<p>The following method should return the DateTime that you want.  Pass in true for Sunday being the first day of the week, false for Monday:</p>\n\n<pre><code>private DateTime getStartOfWeek(bool useSunday)\n{\n    DateTime now = DateTime.Now;\n    int dayOfWeek = (int)now.DayOfWeek;\n\n    if(!useSunday)\n        dayOfWeek--;\n\n    if(dayOfWeek &lt; 0)\n    {// day of week is Sunday and we want to use Monday as the start of the week\n    // Sunday is now the seventh day of the week\n        dayOfWeek = 6;\n    }\n\n    return now.AddDays(-1 * (double)dayOfWeek);\n}\n</code></pre>\n'}, {'answer_id': 38060, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 4, 'selected': False, 'text': "<p>This may be a bit of a hack, but you can cast the .DayOfWeek property to an int (it's an enum and since its not had its underlying data type changed it defaults to int) and use that to determine the previous start of the week.</p>\n\n<p>It appears the week specified in the DayOfWeek enum starts on Sunday, so if we subtract 1 from this value that'll be equal to how many days the Monday is before the current date. We also need to map the Sunday (0) to equal 7 so given 1 - 7 = -6 the Sunday will map to the previous Monday:-</p>\n\n<pre><code>DateTime now = DateTime.Now;\nint dayOfWeek = (int)now.DayOfWeek;\ndayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;\nDateTime startOfWeek = now.AddDays(1 - (int)now.DayOfWeek);\n</code></pre>\n\n<p>The code for the previous Sunday is simpler as we don't have to make this adjustment:-</p>\n\n<pre><code>DateTime now = DateTime.Now;\nint dayOfWeek = (int)now.DayOfWeek;\nDateTime startOfWeek = now.AddDays(-(int)now.DayOfWeek);\n</code></pre>\n"}, {'answer_id': 38064, 'author': 'Compile This', 'author_id': 4048, 'author_profile': 'https://Stackoverflow.com/users/4048', 'pm_score': 11, 'selected': True, 'text': '<p>Use an extension method:</p>\n<pre><code>public static class DateTimeExtensions\n{\n    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)\n    {\n        int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;\n        return dt.AddDays(-1 * diff).Date;\n    }\n}\n</code></pre>\n<p>Which can be used as follows:</p>\n<pre><code>DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday);\nDateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);\n</code></pre>\n'}, {'answer_id': 38067, 'author': 'Jason Navarrete', 'author_id': 3920, 'author_profile': 'https://Stackoverflow.com/users/3920', 'pm_score': 6, 'selected': False, 'text': '<p>A little more verbose and culture-aware:</p>\n\n<pre><code>System.Globalization.CultureInfo ci = \n    System.Threading.Thread.CurrentThread.CurrentCulture;\nDayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;\nDayOfWeek today = DateTime.Now.DayOfWeek;\nDateTime sow = DateTime.Now.AddDays(-(today - fdow)).Date;\n</code></pre>\n'}, {'answer_id': 38076, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 2, 'selected': False, 'text': '<p>This would give you midnight on the first Sunday of the week:</p>\n\n<pre><code>DateTime t = DateTime.Now;\nt -= new TimeSpan ((int) t.DayOfWeek, t.Hour, t.Minute, t.Second);\n</code></pre>\n\n<p>This gives you the first Monday at midnight:</p>\n\n<pre><code>DateTime t = DateTime.Now;\nt -= new TimeSpan ((int) t.DayOfWeek - 1, t.Hour, t.Minute, t.Second);\n</code></pre>\n'}, {'answer_id': 38137, 'author': 'Domenic', 'author_id': 3191, 'author_profile': 'https://Stackoverflow.com/users/3191', 'pm_score': 1, 'selected': False, 'text': '<p>You could use the excellent <a href="http://www.codeplex.com/umbrella" rel="nofollow noreferrer">Umbrella library</a>:</p>\n\n<pre><code>using nVentive.Umbrella.Extensions.Calendar;\nDateTime beginning = DateTime.Now.BeginningOfWeek();\n</code></pre>\n\n<p>However, they <em>do</em> seem to have stored Monday as the first day of the week (see the property <code>nVentive.Umbrella.Extensions.Calendar.DefaultDateTimeCalendarExtensions.WeekBeginsOn</code>), so that previous localized solution is a bit better. Unfortunate.</p>\n\n<p><strong>Edit</strong>: looking closer at the question, it looks like Umbrella might actually work for that too:</p>\n\n<pre><code>// Or DateTime.Now.PreviousDay(DayOfWeek.Monday)\nDateTime monday = DateTime.Now.PreviousMonday(); \nDateTime sunday = DateTime.Now.PreviousSunday();\n</code></pre>\n\n<p>Although it\'s worth noting that if you ask for the previous Monday on a Monday, it\'ll give you seven days back. But this is also true if you use <code>BeginningOfWeek</code>, which seems like a bug :(.</p>\n'}, {'answer_id': 38406, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 4, 'selected': False, 'text': "<p>Let's combine the culture-safe answer and the extension method answer:</p>\n\n<pre><code>public static class DateTimeExtensions\n{\n    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)\n    {\n        System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;\n        DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;\n        return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek- fdow));\n    }\n}\n</code></pre>\n"}, {'answer_id': 1378914, 'author': 'Simon', 'author_id': 53158, 'author_profile': 'https://Stackoverflow.com/users/53158', 'pm_score': 5, 'selected': False, 'text': '<p>Using <a href="https://github.com/FluentDateTime/FluentDateTime" rel="noreferrer">Fluent DateTime</a>:</p>\n\n<pre><code>var monday = DateTime.Now.Previous(DayOfWeek.Monday);\nvar sunday = DateTime.Now.Previous(DayOfWeek.Sunday);\n</code></pre>\n'}, {'answer_id': 1830189, 'author': 'mails2008', 'author_id': 222558, 'author_profile': 'https://Stackoverflow.com/users/222558', 'pm_score': -1, 'selected': False, 'text': '<pre><code>public static System.DateTime getstartweek()\n{\n    System.DateTime dt = System.DateTime.Now;\n    System.DayOfWeek dmon = System.DayOfWeek.Monday;\n    int span = dt.DayOfWeek - dmon;\n    dt = dt.AddDays(-span);\n    return dt;\n}\n</code></pre>\n'}, {'answer_id': 2883306, 'author': 'user324365', 'author_id': 324365, 'author_profile': 'https://Stackoverflow.com/users/324365', 'pm_score': 2, 'selected': False, 'text': '<p>Thanks for the examples. I needed to always use the "CurrentCulture" first day of the week and for an array I needed to know the exact Daynumber.. so here are my first extensions:</p>\n\n<pre><code>public static class DateTimeExtensions\n{\n    //http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week\n    //http://stackoverflow.com/questions/1788508/calculate-date-with-monday-as-dayofweek1\n    public static DateTime StartOfWeek(this DateTime dt)\n    {\n        //difference in days\n        int diff = (int)dt.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; //sunday=always0, monday=always1, etc.\n\n        //As a result we need to have day 0,1,2,3,4,5,6 \n        if (diff &lt; 0)\n        {\n            diff += 7;\n        }\n        return dt.AddDays(-1 * diff).Date;\n    }\n\n    public static int DayNoOfWeek(this DateTime dt)\n    {\n        //difference in days\n        int diff = (int)dt.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; //sunday=always0, monday=always1, etc.\n\n        //As a result we need to have day 0,1,2,3,4,5,6 \n        if (diff &lt; 0)\n        {\n            diff += 7;\n        }\n        return diff + 1; //Make it 1..7\n    }\n}\n</code></pre>\n'}, {'answer_id': 2926472, 'author': 'Zamir', 'author_id': 352544, 'author_profile': 'https://Stackoverflow.com/users/352544', 'pm_score': 0, 'selected': False, 'text': '<p>This will return both the beginning of the week and the end of the week dates: </p>\n\n<pre><code>    private string[] GetWeekRange(DateTime dateToCheck)\n    {\n        string[] result = new string[2];\n        TimeSpan duration = new TimeSpan(0, 0, 0, 0); //One day \n        DateTime dateRangeBegin = dateToCheck;\n        DateTime dateRangeEnd = DateTime.Today.Add(duration);\n\n        dateRangeBegin = dateToCheck.AddDays(-(int)dateToCheck.DayOfWeek);\n        dateRangeEnd = dateToCheck.AddDays(6 - (int)dateToCheck.DayOfWeek);\n\n        result[0] = dateRangeBegin.Date.ToString();\n        result[1] = dateRangeEnd.Date.ToString();\n        return result;\n\n    }\n</code></pre>\n\n<p>I have posted the complete code for calculating the begin/end of week, month, quarter and year on my blog\n<a href="http://zamirsblog.blogspot.com/2010/02/find-first-and-last-date-of-given-week.html" rel="nofollow noreferrer">ZamirsBlog</a></p>\n'}, {'answer_id': 10553208, 'author': 'Janspeed', 'author_id': 1343550, 'author_profile': 'https://Stackoverflow.com/users/1343550', 'pm_score': 5, 'selected': False, 'text': '<p>Ugly but it at least gives the right dates back </p>\n\n<p>With start of week set by system:</p>\n\n<pre><code>    public static DateTime FirstDateInWeek(this DateTime dt)\n    {\n        while (dt.DayOfWeek != System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek)\n            dt = dt.AddDays(-1);\n        return dt;\n    }\n</code></pre>\n\n<p>Without:</p>\n\n<pre><code>    public static DateTime FirstDateInWeek(this DateTime dt, DayOfWeek weekStartDay)\n    {\n        while (dt.DayOfWeek != weekStartDay)\n            dt = dt.AddDays(-1);\n        return dt;\n    }\n</code></pre>\n'}, {'answer_id': 10685884, 'author': 'yeasir007', 'author_id': 1326559, 'author_profile': 'https://Stackoverflow.com/users/1326559', 'pm_score': 2, 'selected': False, 'text': '<p>Try with this in C#. With this code you can get both the first date and last date of a given week. Here Sunday is the first day and Saturday is the last day, but you can set both days according to your culture.</p>\n<pre><code>DateTime firstDate = GetFirstDateOfWeek(DateTime.Parse(&quot;05/09/2012&quot;).Date, DayOfWeek.Sunday);\nDateTime lastDate = GetLastDateOfWeek(DateTime.Parse(&quot;05/09/2012&quot;).Date, DayOfWeek.Saturday);\n\npublic static DateTime GetFirstDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay)\n{\n    DateTime firstDayInWeek = dayInWeek.Date;\n    while (firstDayInWeek.DayOfWeek != firstDay)\n        firstDayInWeek = firstDayInWeek.AddDays(-1);\n\n    return firstDayInWeek;\n}\n\npublic static DateTime GetLastDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay)\n{\n    DateTime lastDayInWeek = dayInWeek.Date;\n    while (lastDayInWeek.DayOfWeek != firstDay)\n        lastDayInWeek = lastDayInWeek.AddDays(1);\n\n    return lastDayInWeek;\n}\n</code></pre>\n'}, {'answer_id': 11379841, 'author': 'Matthew Hintzen', 'author_id': 597406, 'author_profile': 'https://Stackoverflow.com/users/597406', 'pm_score': 3, 'selected': False, 'text': '<p>Putting it all together, with Globalization and allowing for specifying the first day of the week as part of the call we have</p>\n\n<pre><code>public static DateTime StartOfWeek ( this DateTime dt, DayOfWeek? firstDayOfWeek )\n{\n    DayOfWeek fdow;\n\n    if ( firstDayOfWeek.HasValue  )\n    {\n        fdow = firstDayOfWeek.Value;\n    }\n    else\n    {\n        System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;\n        fdow = ci.DateTimeFormat.FirstDayOfWeek;\n    }\n\n    int diff = dt.DayOfWeek - fdow;\n\n    if ( diff &lt; 0 )\n    {\n        diff += 7;\n    }\n\n    return dt.AddDays( -1 * diff ).Date;\n\n}\n</code></pre>\n'}, {'answer_id': 14609184, 'author': 'Eric', 'author_id': 408879, 'author_profile': 'https://Stackoverflow.com/users/408879', 'pm_score': 7, 'selected': False, 'text': '<p>The quickest way I can come up with is:</p>\n<pre><code>var sunday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);\n</code></pre>\n<p>If you would like any other day of the week to be your start date, all you need to do is add the DayOfWeek value to the end</p>\n<pre><code>var monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday);\n\nvar tuesday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Tuesday);\n</code></pre>\n'}, {'answer_id': 15224122, 'author': 'Andreas Kromann', 'author_id': 2135748, 'author_profile': 'https://Stackoverflow.com/users/2135748', 'pm_score': 2, 'selected': False, 'text': '<p>Here is a <em>correct</em> solution. The following code works regardless if the first day of the week is a Monday or a Sunday or something else.</p>\n<pre><code>public static class DateTimeExtension\n{\n  public static DateTime GetFirstDayOfThisWeek(this DateTime d)\n  {\n    CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;\n    var first = (int)ci.DateTimeFormat.FirstDayOfWeek;\n    var current = (int)d.DayOfWeek;\n\n    var result = first &lt;= current ?\n      d.AddDays(-1 * (current - first)) :\n      d.AddDays(first - current - 7);\n\n    return result;\n  }\n}\n\nclass Program\n{\n  static void Main()\n  {\n    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(&quot;en-US&quot;);\n    Console.WriteLine(&quot;Current culture set to en-US&quot;);\n    RunTests();\n    Console.WriteLine();\n    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(&quot;da-DK&quot;);\n    Console.WriteLine(&quot;Current culture set to da-DK&quot;);\n    RunTests();\n    Console.ReadLine();\n  }\n\n  static void RunTests()\n  {\n    Console.WriteLine(&quot;Today {1}: {0}&quot;, DateTime.Today.Date.GetFirstDayOfThisWeek(), DateTime.Today.Date.ToString(&quot;yyyy-MM-dd&quot;));\n    Console.WriteLine(&quot;Saturday 2013-03-02: {0}&quot;, new DateTime(2013, 3, 2).GetFirstDayOfThisWeek());\n    Console.WriteLine(&quot;Sunday 2013-03-03: {0}&quot;, new DateTime(2013, 3, 3).GetFirstDayOfThisWeek());\n    Console.WriteLine(&quot;Monday 2013-03-04: {0}&quot;, new DateTime(2013, 3, 4).GetFirstDayOfThisWeek());\n  }\n}\n</code></pre>\n'}, {'answer_id': 18821567, 'author': 'HelloWorld', 'author_id': 1394710, 'author_profile': 'https://Stackoverflow.com/users/1394710', 'pm_score': 3, 'selected': False, 'text': '<pre><code>var now = System.DateTime.Now;\n\nvar result = now.AddDays(-((now.DayOfWeek - System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7)).Date;\n</code></pre>\n'}, {'answer_id': 26167621, 'author': 'Denis', 'author_id': 495799, 'author_profile': 'https://Stackoverflow.com/users/495799', 'pm_score': 0, 'selected': False, 'text': '<pre><code>    namespace DateTimeExample\n    {\n        using System;\n\n        public static class DateTimeExtension\n        {\n            public static DateTime GetMonday(this DateTime time)\n            {\n                if (time.DayOfWeek != DayOfWeek.Monday)\n                    return GetMonday(time.AddDays(-1)); //Recursive call\n\n                return time;\n            }\n        }\n\n        internal class Program\n        {\n            private static void Main()\n            {\n                Console.WriteLine(DateTime.Now.GetMonday());\n                Console.ReadLine();\n            }\n        }\n    } \n</code></pre>\n'}, {'answer_id': 29106459, 'author': 'Adrian', 'author_id': 4682104, 'author_profile': 'https://Stackoverflow.com/users/4682104', 'pm_score': -1, 'selected': False, 'text': '<pre><code>   d = DateTime.Now;\n            int dayofweek =(int) d.DayOfWeek;\n            if (dayofweek != 0)\n            {\n                d = d.AddDays(1 - dayofweek);\n            }\n            else { d = d.AddDays(-6); }\n</code></pre>\n'}, {'answer_id': 34108838, 'author': 'Rabbex', 'author_id': 5644465, 'author_profile': 'https://Stackoverflow.com/users/5644465', 'pm_score': 3, 'selected': False, 'text': '<pre><code>using System;\nusing System.Globalization;\n\nnamespace MySpace\n{\n    public static class DateTimeExtention\n    {\n        // ToDo: Need to provide culturaly neutral versions.\n\n        public static DateTime GetStartOfWeek(this DateTime dt)\n        {\n            DateTime ndt = dt.Subtract(TimeSpan.FromDays((int)dt.DayOfWeek));\n            return new DateTime(ndt.Year, ndt.Month, ndt.Day, 0, 0, 0, 0);\n        }\n\n        public static DateTime GetEndOfWeek(this DateTime dt)\n        {\n            DateTime ndt = dt.GetStartOfWeek().AddDays(6);\n            return new DateTime(ndt.Year, ndt.Month, ndt.Day, 23, 59, 59, 999);\n        }\n\n        public static DateTime GetStartOfWeek(this DateTime dt, int year, int week)\n        {\n            DateTime dayInWeek = new DateTime(year, 1, 1).AddDays((week - 1) * 7);\n            return dayInWeek.GetStartOfWeek();\n        }\n\n        public static DateTime GetEndOfWeek(this DateTime dt, int year, int week)\n        {\n            DateTime dayInWeek = new DateTime(year, 1, 1).AddDays((week - 1) * 7);\n            return dayInWeek.GetEndOfWeek();\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 34353217, 'author': 'Piotr Lewandowski', 'author_id': 5694667, 'author_profile': 'https://Stackoverflow.com/users/5694667', 'pm_score': 2, 'selected': False, 'text': '<p>Modulo in C# works bad for -1 mod 7 (it should be 6, but C# returns -1)\nso... a &quot;one-liner&quot; solution to this will look like this :)</p>\n<pre><code>private static DateTime GetFirstDayOfWeek(DateTime date)\n{\n    return date.AddDays(\n        -(((int)date.DayOfWeek - 1) -\n          (int)Math.Floor((double)((int)date.DayOfWeek - 1) / 7) * 7));\n}\n</code></pre>\n'}, {'answer_id': 37902263, 'author': 'Threezool', 'author_id': 5131763, 'author_profile': 'https://Stackoverflow.com/users/5131763', 'pm_score': 2, 'selected': False, 'text': '<p>I tried several, but I 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:</p>\n<pre><code>int delta = DayOfWeek.Monday - DateTime.Now.DayOfWeek;\nDateTime monday = DateTime.Now.AddDays(delta == 1 ? -6 : delta);\nreturn monday;\n</code></pre>\n'}, {'answer_id': 43684154, 'author': 'F.H.', 'author_id': 2906568, 'author_profile': 'https://Stackoverflow.com/users/2906568', 'pm_score': 2, 'selected': False, 'text': '<p>The same for end of the week (in style of <a href="https://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week/38064#38064">Compile This\'s answer</a>):</p>\n<pre><code>    public static DateTime EndOfWeek(this DateTime dt)\n    {\n        int diff = 7 - (int)dt.DayOfWeek;\n\n        diff = diff == 7 ? 0 : diff;\n\n        DateTime eow = dt.AddDays(diff).Date;\n\n        return new DateTime(eow.Year, eow.Month, eow.Day, 23, 59, 59, 999) { };\n    }\n</code></pre>\n'}, {'answer_id': 48411638, 'author': 'Mike', 'author_id': 7612816, 'author_profile': 'https://Stackoverflow.com/users/7612816', 'pm_score': 0, 'selected': False, 'text': '<p>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 maximum flexibility and reuse.</p>\n<pre><code>/// &lt;summary&gt;\n/// Gets the date of the first day of the week for the date.\n/// &lt;/summary&gt;\n/// &lt;param name=&quot;date&quot;&gt;The date to be used&lt;/param&gt;\n/// &lt;param name=&quot;cultureInfo&quot;&gt;If none is provided, the current culture is used&lt;/param&gt;\n/// &lt;returns&gt;The date of the beggining of the week based on the culture specifed&lt;/returns&gt;\npublic static DateTime StartOfWeek(this DateTime date, CultureInfo cultureInfo=null) =&gt;\n    date.AddDays(-1 * (7 + (date.DayOfWeek - (cultureInfo ?? CultureInfo.CurrentCulture).DateTimeFormat.FirstDayOfWeek)) % 7).Date;\n</code></pre>\n<p>Example Usage:</p>\n<pre><code>public static void TestFirstDayOfWeekExtension() {\n    DateTime date = DateTime.Now;\n    foreach(System.Globalization.CultureInfo culture in CultureInfo.GetCultures(CultureTypes.UserCustomCulture | CultureTypes.SpecificCultures)) {\n        Console.WriteLine($&quot;{culture.EnglishName}: {date.ToShortDateString()} First Day of week: {date.StartOfWeek(culture).ToShortDateString()}&quot;);\n    }\n}\n</code></pre>\n'}, {'answer_id': 48494983, 'author': 'Ali Umair', 'author_id': 2156152, 'author_profile': 'https://Stackoverflow.com/users/2156152', 'pm_score': 0, 'selected': False, 'text': '<p>If you want Saturday or Sunday or any day of week, but not exceeding the current week (Sat-Sun), I got you covered with this piece of code.</p>\n<pre><code>public static DateTime GetDateInCurrentWeek(this DateTime date, DayOfWeek day)\n{\n    var temp = date;\n    var limit = (int)date.DayOfWeek;\n    var returnDate = DateTime.MinValue;\n\n    if (date.DayOfWeek == day) \n        return date;\n\n    for (int i = limit; i &lt; 6; i++)\n    {\n        temp = temp.AddDays(1);\n\n        if (day == temp.DayOfWeek)\n        {\n            returnDate = temp;\n            break;\n        }\n    }\n    if (returnDate == DateTime.MinValue)\n    {\n        for (int i = limit; i &gt; -1; i++)\n        {\n            date = date.AddDays(-1);\n\n            if (day == date.DayOfWeek)\n            {\n                returnDate = date;\n                break;\n            }\n        }\n    }\n    return returnDate;\n}\n</code></pre>\n'}, {'answer_id': 52720346, 'author': 'George Stavrou', 'author_id': 6050375, 'author_profile': 'https://Stackoverflow.com/users/6050375', 'pm_score': 4, 'selected': False, 'text': '<p><strong>For Monday</strong></p>\n\n<pre><code>DateTime startAtMonday = DateTime.Now.AddDays(DayOfWeek.Monday - DateTime.Now.DayOfWeek);\n</code></pre>\n\n<p><strong>For Sunday</strong></p>\n\n<pre><code>DateTime startAtSunday = DateTime.Now.AddDays(DayOfWeek.Sunday- DateTime.Now.DayOfWeek);\n</code></pre>\n'}, {'answer_id': 53518108, 'author': 'pixelda', 'author_id': 1489673, 'author_profile': 'https://Stackoverflow.com/users/1489673', 'pm_score': 0, 'selected': False, 'text': "<p>We like one-liners: Get the difference between the current culture's first day of week and the current day, and then subtract the number of days from the current day:</p>\n<pre><code>var weekStartDate = DateTime.Now.AddDays(-((int)now.DayOfWeek - (int)DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek));\n</code></pre>\n"}, {'answer_id': 55514986, 'author': 'Muhammad Abbas', 'author_id': 5834683, 'author_profile': 'https://Stackoverflow.com/users/5834683', 'pm_score': 3, 'selected': False, 'text': '<p>Step 1:</p>\n<p>Create a static class</p>\n<pre><code>public static class TIMEE\n{\n    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)\n    {\n        int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;\n        return dt.AddDays(-1 * diff).Date;\n    }\n\n    public static DateTime EndOfWeek(this DateTime dt, DayOfWeek startOfWeek)\n    {\n        int diff = (7 - (dt.DayOfWeek - startOfWeek)) % 7;\n        return dt.AddDays(1 * diff).Date;\n    }\n}\n</code></pre>\n<p>Step 2: Use this class to get both start and end day of the week</p>\n<pre><code>DateTime dt = TIMEE.StartOfWeek(DateTime.Now ,DayOfWeek.Monday);\nDateTime dt1 = TIMEE.EndOfWeek(DateTime.Now, DayOfWeek.Sunday);\n</code></pre>\n'}, {'answer_id': 57274114, 'author': 'Rowan Richards', 'author_id': 5821656, 'author_profile': 'https://Stackoverflow.com/users/5821656', 'pm_score': 1, 'selected': False, 'text': '<p>Following on from <a href="https://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week/38064#38064">Compile This\' answer</a>, use the following method to obtain the date for any day of the week:</p>\n<pre><code>public static DateTime GetDayOfWeek(DateTime dateTime, DayOfWeek dayOfWeek)\n{\n   var monday = dateTime.Date.AddDays((7 + (dateTime.DayOfWeek - DayOfWeek.Monday) % 7) * -1);\n\n   var diff = dayOfWeek - DayOfWeek.Monday;\n\n   if (diff == -1)\n   {\n      diff = 6;\n   }\n\n   return monday.AddDays(diff);\n}\n</code></pre>\n'}, {'answer_id': 60892025, 'author': 'mihauuuu', 'author_id': 8549646, 'author_profile': 'https://Stackoverflow.com/users/8549646', 'pm_score': -1, 'selected': False, 'text': '<p>Try to create a function which uses recursion. Your DateTime object is an input and function returns a new DateTime object which stands for the beginning of the week. </p>\n\n<pre><code>    DateTime WeekBeginning(DateTime input)\n    {\n        do\n        {\n            if (input.DayOfWeek.ToString() == "Monday")\n                return input;\n            else\n                return WeekBeginning(input.AddDays(-1));\n        } while (input.DayOfWeek.ToString() == "Monday");\n    }\n</code></pre>\n'}, {'answer_id': 61554096, 'author': 'C. Keats', 'author_id': 8273351, 'author_profile': 'https://Stackoverflow.com/users/8273351', 'pm_score': 0, 'selected': False, 'text': '<p>Calculating this way lets you choose which day of the week indicates the start of a new week (in the example I chose Monday).</p>\n\n<p>Note that doing this calculation for a day that is a Monday will give the <em>current</em> Monday and not the previous one.</p>\n\n<pre><code>//Replace with whatever input date you want\nDateTime inputDate = DateTime.Now;\n\n//For this example, weeks start on Monday\nint startOfWeek = (int)DayOfWeek.Monday;\n\n//Calculate the number of days it has been since the start of the week\nint daysSinceStartOfWeek = ((int)inputDate.DayOfWeek + 7 - startOfWeek) % 7;\n\nDateTime previousStartOfWeek = inputDate.AddDays(-daysSinceStartOfWeek);\n</code></pre>\n'}, {'answer_id': 64763247, 'author': 'DReact', 'author_id': 11487686, 'author_profile': 'https://Stackoverflow.com/users/11487686', 'pm_score': 2, 'selected': False, 'text': "<p>I did it for Monday, but with similar logic for Sunday.</p>\n<pre><code>public static DateTime GetStartOfWeekDate()\n{\n    // Get today's date\n    DateTime today = DateTime.Today;\n    // Get the value for today. DayOfWeek is an enum with 0 being Sunday, 1 Monday, etc\n    var todayDayOfWeek = (int)today.DayOfWeek;\n\n    var dateStartOfWeek = today;\n    // If today is not Monday, then get the date for Monday\n    if (todayDayOfWeek != 1)\n    {\n        // How many days to get back to Monday from today\n        var daysToStartOfWeek = (todayDayOfWeek - 1);\n        // Subtract from today's date the number of days to get to Monday\n        dateStartOfWeek = today.AddDays(-daysToStartOfWeek);\n    }\n\n    return dateStartOfWeek;\n\n}\n</code></pre>\n"}, {'answer_id': 68382467, 'author': 'Noob', 'author_id': 8604852, 'author_profile': 'https://Stackoverflow.com/users/8604852', 'pm_score': -1, 'selected': False, 'text': '<p>I did it like this:</p>\n<pre><code>DateTime.Now.Date.AddDays(-(DateTime.Now.Date.DayOfWeek == 0 ? 7 : (int)DateTime.Now.Date.DayOfWeek) + 1)\n</code></pre>\n<p>All this code does is subtracting a number of days from the given datetime.</p>\n<p>If day of week is 0(sunday) then subtract 7 else subtract the day of the week.</p>\n<p>Then add 1 day to the result of the previous line, which gives you the monday of that date.</p>\n<p>This way you can play around with the number(1) at the end to get the desired day.</p>\n<pre><code>private static DateTime GetDay(DateTime date, int daysAmount = 1)\n{\n    return date.Date.AddDays(-(date.Date.DayOfWeek == 0 ? 7 : (int)date.Date.DayOfWeek) + daysAmount);\n}\n</code></pre>\n<p>If you really want to use the DayOfWeek enum then something like this can be used... though I presonally prefer the above one, as I can add or subtract any amount of days.</p>\n<pre><code>private static DateTime GetDayOfWeek(DateTime date, DayOfWeek dayOfWeek = DayOfWeek.Monday)\n{\n    return date.Date.AddDays(-(date.Date.DayOfWeek == 0 ? 7 : (int)date.Date.DayOfWeek) + (dayOfWeek == 0 ? 7 : (int)dayOfWeek));\n}\n</code></pre>\n'}, {'answer_id': 72867015, 'author': 'Display Name', 'author_id': 2815956, 'author_profile': 'https://Stackoverflow.com/users/2815956', 'pm_score': 0, 'selected': False, 'text': '<p>I work with a lot of schools, so correctly using Monday as the first day of the week is important here.</p>\n<p>A lot of the most terse answers here don\'t work on Sunday -- we often end up returning the date of tomorrow on Sunday, which is not good for running a report on last week\'s activities.</p>\n<p>Here\'s my solution, which returns last Monday on Sunday, and today on Monday.</p>\n<pre class="lang-cpp prettyprint-override"><code>// Adding 7 so remainder is always positive; Otherwise % returns -1 on Sunday.\nvar daysToSubtract = (7 + (int)today.DayOfWeek - (int)DayOfWeek.Monday) % 7;\n\nvar monday = today\n    .AddDays(-daysToSubtract)\n    .Date;\n</code></pre>\n<p>Remember to use a method parameter for &quot;today&quot; so it\'s unit testable!!</p>\n'}]
['https://Stackoverflow.com/questions/38039', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/383/']