1

How can I get the date and week number of each week between two dates.

For example

  • Start date : 2019-10-01
  • End date : 2020-02-01

So the following dates should be in the list.

[Week:40] 2019-10-01 ~ 2019-10-06
[Week:41] 2019-10-07 ~ 2019-10-13
[Week:42] 2019-10-14 ~ 2019-10-20
[Week:43] 2019-10-21 ~ 2019-10-27
[Week:44] 2019-10-28 ~ 2019-11-03
[Week:45] 2019-11-04 ~ 2019-11-10
[Week:46] 2019-11-11 ~ 2019-11-17
[Week:47] 2019-11-18 ~ 2019-11-24
[Week:48] 2019-11-25 ~ 2019-12-01
[Week:49] 2019-12-02 ~ 2019-12-08
[Week:50] 2019-12-09 ~ 2019-12-15
[Week:51] 2019-12-16 ~ 2019-12-22
[Week:52] 2019-12-23 ~ 2019-12-29
[Week:53] 2019-12-30 ~ 2019-12-31
[Week:1] 2020-01-01 ~ 2020-01-05
[Week:2] 2020-01-06 ~ 2020-01-12
[Week:3] 2020-01-13 ~ 2020-01-19
[Week:4] 2020-01-20 ~ 2020-01-26
[Week:5] 2020-01-27 ~ 2020-02-01

This my code:

var date = new DateTime(DateTime.Today.Year, 1, 1);
var startOfFirstWeek = date.AddDays(1 - (int)(date.DayOfWeek));
var weeks =
    Enumerable
      .Range(0, 54)
      .Select(i => new {
          weekStart = startOfFirstWeek.AddDays(i * 7)
       })
      .TakeWhile(x => x.weekStart.Year <= date.Year)
      .Select(x => new {
          x.weekStart,
          weekFinish = x.weekStart.AddDays(6)
       })
      .SkipWhile(x => x.weekFinish < date.AddDays(1))
      .Select((x, i) => new {
          x.weekStart,
          x.weekFinish,
          weekNum = i + 1
      });
Chia Wei
  • 13
  • 7

1 Answers1

1

It's rather complicated to use LINQ for this. Loops would probably be simpler:

var st = new DateTime(2019, 7, 1);
var ed = new DateTime(2020,6,1);
var ey = new DateTime(st.Year,12,31);


while(st <= ed){
  var e = st.AddDays(6 - Math.Min((int)st.DayOfWeek,6));
  if(st.Year < e.Year)
    e = ey;
  if(e > ed)
e = ed;

  Console.Write($"[Week:{((st.DayOfYear-1)/7)+1}] {st:yyyy-MM-dd} ~ {e:yyyy-MM-dd}");

    st = e.AddDays(1);
}

The logic here is to take the start date and use the day of week it is to calculate the end date for that week. Mostly this is 6 days after the start date but the first iteration can be different

Then we check if the year change and peg the end date to the year end if it did

Then we check if we are past the end date and peg it to the end date if we are

Print/add/whatever

Then go again by bumping into the next week for the start date, which also helps the loop control

Note: Week Of Year Code can be calendar dependent or interpreted differently - see something like Week of Year C# Datetime to see how to calc for Gregorian or perhaps take a basic approach of ((DayOfYear-1)/7)+1


I'm just not sure what your logic was that reduced the output to 4 lines, did you intend to give an example line 2 that was the end of the year to make end of year treatment obvious?

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I’m just showing an example, normally he will show all dates. – Chia Wei Jun 07 '20 at 09:06
  • Ah, sorry.. I forgot that WeekOfYear is not a thing unless you write an extension method for it. See https://stackoverflow.com/questions/10102714/week-of-year-c-sharp-datetime . The rest of the code works out ok in terms of giving you the dates for your list, just add a biggest of code that calls the week of the year according to your chosen calendar ( I'm not sure what that is) – Caius Jard Jun 07 '20 at 09:18