Choose only the fourth Sunday of each month

I’m stuck somewhere now, now I need your help.

I want to show in the drop-down list only the fourth Sunday of each month, say, from September 1 to August 31, 2011.

I need only the fourth Sunday in the dropdown how to do this using asp.net C #

Hi

+3
source share
3 answers

Here's an approach that uses a bit of LINQ and the knowledge that the fourth Sunday will be held between 22 and 28 months. inclusive.

DateTime startDate = new DateTime(2010, 9, 1);
DateTime endDate = startDate.AddYears(1).AddDays(-1);

List<DateTime> fourthSundays = new List<DateTime>();

DateTime currentDate = startDate;
while (currentDate < endDate)
{
    // we know the fourth sunday will be the 22-28
    DateTime fourthSunday = Enumerable.Range(22, 7).Select(day => new DateTime(currentDate.Year, currentDate.Month, day)).Single(date => date.DayOfWeek == DayOfWeek.Sunday);
    fourthSundays.Add(fourthSunday);
    currentDate = currentDate.AddMonths(1);
}

Then you can link this List<DateTime>to the drop-down list or skip the list itself in favor of adding items when creating them in the drop-down list, for example below.

yourDropdown.Items.Add(new ListItem(fourthSunday.ToString()));

LINQ ( ) .

DateTime startDate = new DateTime(2010, 9, 1); 
IEnumerable<DateTime> fourthSundays =
    Enumerable.Range(0, 12)
    .Select(item => startDate.AddMonths(item))
    .Select(currentMonth =>
        Enumerable.Range(22, 7)
        .Select(day => new DateTime(currentMonth.Year, currentMonth.Month, day))
        .Single(date => date.DayOfWeek == DayOfWeek.Sunday)
    );
+8

, . , ,

class Program
{
    static void Main(string[] args)
    {
        DateTime startDate = new DateTime(2010, 09, 1);
        foreach(DateTime dt in EachMonth( new DateTime(2010, 09, 1), new DateTime(2011, 09, 1))){
            DateTime? result = GetDayByWeekOffset(DayOfWeek.Sunday, dt, 4);
           Console.WriteLine("Sunday:" + (result.HasValue?result.Value.ToString("MM-dd-yyyy"):"null"));
        }
        Console.ReadKey();
    }

    public static DateTime? GetDayByWeekOffset(DayOfWeek day, DateTime month, int weekOffSet)
    {
        //First day of month
        DateTime firstDayOfMonth = month.AddDays((-1 * month.Day) + 1);

        // 
        int daysOffSet;
        daysOffSet= ((int)day + 7 - (int)firstDayOfMonth.DayOfWeek) % 7;
        DateTime firstDay = month.AddDays(daysOffSet);

        // Add the number of weeks specified
        DateTime resultDate = firstDay.AddDays((weekOffSet - 1) * 7);

        if (resultDate.Month != firstDayOfMonth.Month){
            return null;
        }else{
            return resultDate;
        }
    }

    public static IEnumerable<DateTime> EachMonth(DateTime from, DateTime thru)
    {
        for (var month = from.Date; month.Date <= thru.Date; month = month.AddMonths(1))
            yield return month;
    }
}
+1

Anthony's answer above is good, I like it a lot. Alternatively, this is a method that is parameterized for the day of the week and week number (i.e. if you need other combinations, for example, 4th Sunday, 3rd Friday, etc.) with some comments.

Name it as follows:

List<DateTime> sundays = DateInstances(new DateTime(2010, 9, 1), new DateTime(2011, 8, 31), DayOfWeek.Sunday, 4);

And the method itself:

public List<DateTime> DateInstances(DateTime start, DateTime end, DayOfWeek day, int weeks)
{
    if (start > end)
        throw new ArgumentOutOfRangeException("end", "The start date must occur before the end date");

    List<DateTime> results = new List<DateTime>();

    DateTime temp = start;
    while (temp < end)
    {
        DateTime firstWeekday = new DateTime(temp.Year, temp.Month, 1);

        //increment to the given day (i.e. if we want the 4th sunday, we must find the first sunday of the month)
        while (firstWeekday.DayOfWeek != day)
            firstWeekday = firstWeekday.AddDays(1);

        //add the number of weeks (note: we already have the first instance, so subtract 1)
        firstWeekday = firstWeekday.AddDays(7 * (weeks - 1));

        //make sure we haven't gone over to the next month
        if (firstWeekday.Month == temp.Month)
            results.Add(firstWeekday);

        //let not loop forever ;)
        temp = temp.AddMonths(1);
    }

    return results;
}
0
source

Source: https://habr.com/ru/post/1762568/


All Articles