naveen
naveen

Reputation: 1

How to get past week dates from a list of dates in C#?

In My DB I have a dates like this:

01-03-2022 00:00:00 
07-03-2022 00:00:00 
05-03-2022 00:00:00 
012-03-2022 00:00:00    
013-03-2022 00:00:00    
014-03-2022 00:00:00    

after every week on Monday, I need past 1 week dates to be fetched

I am iterating through dates like this:

    foreach(var item in model.dates)
    {
        // item.Date has all dates
        // I don't know how to get dates here
    }

I want to fetch only those dates, How can I do that?

Upvotes: 0

Views: 79

Answers (1)

fubo
fubo

Reputation: 45947

if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
{
    DateTime weekStart = DateTime.Today.AddDays(-7);
    DateTime weekEnd = DateTime.Today.AddDays(-1);

    var result = model.Dates.Where(x => weekStart <= x && weekEnd >= x);
}

Upvotes: 1

Related Questions