-1

I am new to C# and would like to make a dropdown list containing all Thursday Dates?

I currently have an SQL table with all of these dates, but would rather have them generated in a function and used to populate a dropdown.

Any examples of best approach to this?

Update: I ended up using a calendar bootstrap-datepicker and disabled all days except Thursday’s. This gave me the current month Thursday’s and solved my issue.

jroyce
  • 2,029
  • 2
  • 22
  • 45

3 Answers3

3

You could do something like that where you hard code the first Thursday you want to display, and then set how many Thursdays you want.

        var list = new List<DateTime>();
        DateTime firstThursday = new DateTime(2018,02,20);
        var numberOfThursdayWanted = 1000;
        for (int i = 0; i < numberOfThursdayWanted; ++i)
        {
            list.Add(firstThursday.AddDays(i*7));
        }

        return list;
Dimitri Bosteels
  • 381
  • 4
  • 12
2

You can use LINQ to generate a list of "all":

DateTime firstThursday = DateTime.MinValue.AddDays(Enumerable.Range(0, 7).First(d => DateTime.MinValue.AddDays(d).DayOfWeek == DayOfWeek.Thursday));
int weeks = (int)Math.Ceiling((DateTime.MaxValue - firstThursday).Days / 7.0);
var allThursdays = Enumerable.Range(0, weeks).Select(d => firstThursday.AddDays(d * 7));

Note that allThursdays is just the LINQ query. If doesn't make sense to store all DateTimes in a collection. But maybe you want only those between a specific timespan, f.e.:

DateTime start = DateTime.Today.AddYears(-10);
DateTime end = DateTime.Today.AddYears(10);
DateTime[] allThursDaysInLast10YearsUntilNext10Years = allThursdays
  .Where(d => d >= start && d <= end)
  .ToArray();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    Does this even work? pretty sure DateTime will run out of days before int reaches MaxValue – user6144226 Feb 19 '18 at 15:42
  • I have the same fear. Is DateTime.MaxValue > (7 * int.MaxValue) days + DateTime.MinValue? – Dimitri Bosteels Feb 19 '18 at 15:44
  • @user6144226: it does now with the `weeks` calculation – Tim Schmelter Feb 19 '18 at 15:55
  • Seems so, there might be a one off error somewhere since the last Thursday came out as 12/23/9999 and I do think it could fit 12/30/9999 - but doubt this will be relevant in 7000 thousand years. (Don't even get me started on FirstDayOfWeek). Still good answer and the comment is about int's range is no longer relevant. – user6144226 Feb 19 '18 at 16:07
  • @user6144226: for what it's worth, now it should get "all" – Tim Schmelter Feb 19 '18 at 16:15
1

First you need to somehow narrow your results. For example by year, or by count as Dimitri suggests.

For that you can check this answer: Create an array or List of all dates between two dates

Once you have your IEnumerable<DateTime> you just have to use LINQ to filter the thurdays as follows:

IEnumerable<DateTime> allDateTimes = null; //change this for whatever range you need
IEnumerable<DateTime> onlyThursdays = allDateTimes.Where(d => d.DayOfWeek == DayOfWeek.Thursday);
Pinx0
  • 1,248
  • 17
  • 28