SD7
SD7

Reputation: 514

how to get day of the week only if entered date is falling between last monday

i am trying to get day of the week only if entered date is falling between last Monday to current date i.e if today is 30/10/14 the output should be Thursday else just show date entered here is what i am trying

DateTime aa = Convert.ToDateTime(rr.detail);
if (DateTime.Now.Subtract(aa).Days < 7)
{
    // but this is not i am looking for i want only if aa 
    // is falling between last monday and less than 7 days
}

so any idea how to achieve it?

Upvotes: 1

Views: 128

Answers (1)

keyboardP
keyboardP

Reputation: 69372

If I understand correctly, the steps I'd take are:

1) Get the date of the previous Monday

2) Get the user's date

3) Check if the user's date is after the previous Monday (in which case show the day)

//Get the date of the previous Monday
DateTime prevMonday = DateTime.Now;
while(prevMonday.DayOfWeek != DayOfWeek.Monday)
      prevMonday = prevMonday.AddDays(-1);


//get user's date 
DateTime aa = Convert.ToDateTime(rr.detail);

//check if the user's date is after the Monday
if (aa > prevMonday && aa <= DateTime.Now.Date)    
    Console.WriteLine(aa.DayOfWeek);
else
    Console.WriteLine(aa);

Upvotes: 1

Related Questions