I\'ve been racking my brains over this, but it\'s late on a Friday and I\'m going round in circles.
I need to create a list of working weeks for a drop down list, wi
Just updating what Spender put, because I wanted to make the output of your Datetimes more towards what you wanted.
DateTime jan1 = new DateTime(DateTime.Today.Year, 1, 1);
//beware different cultures, see other answers
DateTime startOfFirstWeek = jan1.AddDays(1 - (int)(jan1.DayOfWeek));
var weeks=
Enumerable
.Range(0,54)
.Select(i => new {
weekStart = startOfFirstWeek.AddDays(i * 7)
})
.TakeWhile(x => x.weekStart.Year <= jan1.Year)
.Select(x => new {
x.weekStart,
weekFinish=x.weekStart.AddDays(4)
})
.SkipWhile(x => x.weekFinish.Year < jan1.Year)
.Select((x,i) => new {
WeekStart = x.weekStart.ToString("ffffdd, d, MMMM"),
WeekFinish = x.weekFinish.ToString("ffffdd, d, MMMM"),
weekNum=i+1
});
The change to correct the formatting to what you wanted is in the last select of the anonymous object.