60

I was wondering if you guys know how to get the date of currents week's monday based on todays date?

i.e 2009-11-03 passed in and 2009-11-02 gets returned back

/M

user2864740
  • 60,010
  • 15
  • 145
  • 220
Lasse Edsvik
  • 9,070
  • 16
  • 73
  • 109

7 Answers7

130

This is what i use (probably not internationalised):

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
DateTime monday = input.AddDays(delta);
Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • 1
    Yes, indeed: Not internationalized. Monday is not always the first day of week. – Serge Wautier Nov 03 '09 at 07:58
  • 16
    You can get the first day of week using: CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek – Shay Erlichmen Nov 03 '09 at 08:03
  • 13
    Actually the first day of the week according to culture is of no interest in this case; the OP wants the monday of the week, not the first day of the week. – Fredrik Mörk Nov 03 '09 at 08:28
  • 10
    -1. Given a Sunday, this returns the next day, rather than 6 days ago. (`DayOfWeek.Monday` is `1`, `input.DayOfWeek` is `0` for a Sunday.) – Rawling Jan 22 '13 at 09:57
  • @Rawling: Hence the "not internationalised" in my answer - in the UK, Sunday is the first day of the week, so the following day is the Monday of that week. – Pondidum Jan 22 '13 at 11:24
  • 2
    Apologies and don't take it personally, I just wanted to voice a dissenting opinion for people who Google "first day of week" and get this. (Interesting that you give Sunday as fdow for UK, as in the `en-GB` culture it's Monday.) – Rawling Jan 22 '13 at 12:26
  • 1
    @Rawling: It's defiantly something to be aware of - why can't it be the same everywhere? :( – Pondidum Jan 22 '13 at 13:28
  • If the 'input' is 1/1/2017 (a Sunday) this code gives 2/1/2017 (the following Monday and the beginning of a new week!) as the first day of the week?!?!?!? – Paul Zahra Aug 26 '16 at 08:52
  • var weekStart = input.AddDays(now.DayOfWeek == DayOfWeek.Sunday ? -6 : DayOfWeek.Monday - input.DayOfWeek); – Paito Jan 08 '20 at 14:21
64

The Pondium answer can search Forward in some case. If you want only Backward search I think it should be:

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
if(delta > 0)
    delta -= 7;
DateTime monday = input.AddDays(delta);
Marco
  • 961
  • 7
  • 11
8

Something like this would work

DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.Monday) dt = dt.AddDays(-1); 

I'm sure there is a nicer way tho :)

PaulB
  • 23,264
  • 14
  • 56
  • 75
6
public static class DateTimeExtension
{
    public static DateTime GetFirstDayOfWeek(this DateTime date)
    {
        var firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

        while (date.DayOfWeek != firstDayOfWeek)
        {
            date = date.AddDays(-1);
        }

        return date;
    }
}

International here. I think as extension it can be more useful.

Arif Dewi
  • 2,222
  • 26
  • 14
3

What about:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek

Why don't use native solution?

Gh61
  • 9,222
  • 4
  • 28
  • 39
1
var now = System.DateTime.Now;

var result = now.AddDays(-((now.DayOfWeek - System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7)).Date;

Probably will return you with Monday. Unless you are using a culture where Monday is not the first day of the week.

HelloWorld
  • 3,381
  • 5
  • 32
  • 58
0

Try this:

public DateTime FirstDayOfWeek(DateTime date)
{
    var candidateDate=date;
    while(candidateDate.DayOfWeek!=DayOfWeek.Monday) {
        candidateDate=candidateDate.AddDays(-1);
    }
    return candidateDate;
}

EDIT for completeness: overload for today's date:

public DateTime FirstDayOfCurrentWeek()
{
    return FirstDayOfWeek(DateTime.Today);
}
Konamiman
  • 49,681
  • 17
  • 108
  • 138