简体   繁体   中英

Get weekday range from two dates

I have a UDF table function in SQL which functions as following

select * from dbo.GetWeekdayRange(convert(datetime, '11/03/2015', 103), convert(datetime, '31/03/2015', 103))

Output:

WeekStartDate              WeekEndDate
-------------------------------------------------
2015-03-11 00:00:00.000   2015-03-15 00:00:00.000
2015-03-16 00:00:00.000   2015-03-22 00:00:00.000
2015-03-23 00:00:00.000   2015-03-29 00:00:00.000
2015-03-30 00:00:00.000   2015-03-31 00:00:00.000

Is there any such function available in C#? I also tried same using LINQ but was not able to succeed. I tried searching it but I did not find any of them.

This can be replicated in C# fairly easily, I'm sure there is a more efficient solution, but this works:

public static IEnumerable<DateRange> GetWeekdayRange(DateTime startDate, DateTime endDate, DayOfWeek weekEndsOn = DayOfWeek.Sunday)
{
    var currStartDate = startDate;
    var currDate = startDate;
    while(currDate<endDate)
    {
        while(currDate.DayOfWeek != weekEndsOn && currDate<endDate)
        {
            currDate = currDate.AddDays(1);
        }
        yield return new DateRange{ WeekStartDate = currStartDate,WeekEndDate = currDate};
        currStartDate = currDate.AddDays(1);
        currDate = currStartDate;
    }
}

public struct DateRange
{
    public DateTime WeekStartDate{get;set;}
    public DateTime WeekEndDate{get;set;}
}

Test code

var start = new DateTime(2015,3,11);
var end = new DateTime(2015,3,31);

var range = GetWeekdayRange(start,end);
foreach(var val in range)
{
    Console.WriteLine("{0:yyyy-MM-dd} - {1:yyyy-MM-dd}",val.WeekStartDate,val.WeekEndDate);
}

Output:

2015-03-11 - 2015-03-15
2015-03-16 - 2015-03-22
2015-03-23 - 2015-03-29
2015-03-30 - 2015-03-31

Live example: http://rextester.com/DGSX37215

if you really want a more LINQ-ey solution, you could do this - I find it harder to read and understand what's going on but it gives the same output. It uses a calendar to group the list of dates into weeks and then selects the Min and Max from the groups

public static IEnumerable<DateRange> GetWeekdayRange(DateTime startDate, DateTime endDate, DayOfWeek weekStartsOn = DayOfWeek.Monday)
{
    var diff = endDate.Subtract(startDate).Days;
    var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
    var range = Enumerable.Range(0,diff+1).Select(v => startDate.AddDays(v))
                        .Select(d => new {Date=d, WeekNumber=cal.GetWeekOfYear(d, System.Globalization.CalendarWeekRule.FirstDay, weekStartsOn) })
                        .GroupBy(g => g.WeekNumber);

    return range.Select(g => new DateRange{WeekStartDate=g.Min(v => v.Date), WeekEndDate= g.Max(v => v.Date)});
}

Live example: http://rextester.com/CXSXC66880

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