简体   繁体   中英

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM