Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get date of first Monday of the week? [duplicate]

Tags:

c#

.net

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

like image 328
Lasse Edsvik Avatar asked Nov 03 '09 07:11

Lasse Edsvik


3 Answers

This is what i use (probably not internationalised):

DateTime input = //...
int delta = DayOfWeek.Monday - input.DayOfWeek;
DateTime monday = input.AddDays(delta);
like image 107
Pondidum Avatar answered Nov 13 '22 08:11

Pondidum


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);
like image 24
Marco Avatar answered Nov 13 '22 09:11

Marco


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 :)

like image 7
PaulB Avatar answered Nov 13 '22 09:11

PaulB