0

I am trying to create a table with some dynamic columns based on week counts of months. I have to create it dynamically, because every year, week numbers of months can be different.

GregorianCalendar _gc = new GregorianCalendar();
string htmlCode = "<table><thead><tr>";

for (int m = 1; m <= 12; m++)
{
    int weekCount = //THAT'S THE MISSING PART
    htmlCode += "<th colspan=\"" + weekCount + "\" style=\"padding: 0; margin: 0; border: none; width: 8.3333%;\">" + m + "</th>";
}

htmlCode += "</tr><tr>";

for (int w = 1; w <= 52; w++)
{
    htmlCode += "<th style=\"padding: 0; margin: 0; border: none; width: 1.923%;\">" + w + "</th>";
}

htmlCode += "</tr></thead></table>";

In first loop, I am creating table's header's first line (months). And in the second loop, I am creating header's second line (weeks). I need week numbers of months for correctly colspan. Output should be something like this:

table {
  width: 1000px;
  text-align: center; margin: 0 auto; padding: 0;
}
<table border="1">
 <thead style="padding: 0; margin: 0; width: 100%;">
  <tr style="padding: 0; margin: 0;">
   <th colspan="5" style="padding: 0; margin: 0; border: none; width: 8.3333%;">JAN</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">FEB</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">MAR</th>
   <th colspan="5" style="padding: 0; margin: 0; border: none; width: 8.3333%;">APR</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">MAY</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">JUN</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">JUL</th>
   <th colspan="5" style="padding: 0; margin: 0; border: none; width: 8.3333%;">AUG</th>
   <th colspan="5" style="padding: 0; margin: 0; border: none; width: 8.3333%;">SEP</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">OCT</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">NOV</th>
   <th colspan="4" style="padding: 0; margin: 0; border: none; width: 8.3333%;">DEC</th>
  </tr>
  <tr style="padding: 0; margin: 0;">
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">5</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">5</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">5</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">5</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
      <th style="padding: 0; margin: 0; border: none; width: 1.923%;">1</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">2</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">3</th>
   <th style="padding: 0; margin: 0; border: none; width: 1.923%;">4</th>
    </tr>
 </thead>
</table>
YSFKBDY
  • 735
  • 1
  • 12
  • 38
  • So what specifically is wrong with what you have? – rory.ap Dec 23 '19 at 15:45
  • My code is missing. I don't know how to calculate week count in every month. – YSFKBDY Dec 23 '19 at 15:46
  • 5
    How do you consider a week in a particular month? Does the week need to start in that month? Contain all the days of the week? What happens when the week spans two months, which month does it get counted in? – Ron Beyer Dec 23 '19 at 15:51
  • https://stackoverflow.com/questions/2136487/calculate-week-of-month-in-net Here is the answer, someone already answered. – Vazgen Khachatryan Dec 23 '19 at 15:52
  • @VazgenKh.I know that answer but it doesn't work for me. I am not giving a date to get week of that date. – YSFKBDY Dec 23 '19 at 15:53
  • 1
    @VillageTech It gives day of week. Not related to my question. – YSFKBDY Dec 23 '19 at 15:54
  • I need to divide 52 weeks to months, long story short. – YSFKBDY Dec 23 '19 at 15:56
  • 1
    @YSFKBDY - we need a longer version of the story. We cannot give you a definitive answer until you give us the info Ron Beyer asks for. For instance, is next week the last week of December 2019, the first week of January 2020, both, or neither? What day of the week does a week start on in your definition? Answer those questions, and you might even talk yourself into a solution. :) – Wonko the Sane Dec 23 '19 at 15:57
  • @WonkotheSane last week of Dec 2019 also counts as first week of Jan 2020. And monday is week start for me. – YSFKBDY Dec 23 '19 at 16:02
  • @YSFKBDY if the month starts with a friday, you don't count that week ? – Cid Dec 23 '19 at 16:03
  • So you'd say there are 6 weeks in Dec 2019? Because you include partial weeks that intersect the month? – Oliver Nicholls Dec 23 '19 at 16:04
  • I just wanted to divide 52 to 12 as correct as possible in current year. That's all. I guess I wasn't aware of your sensible questions. – YSFKBDY Dec 23 '19 at 16:06
  • So Sunday is the last day of the week, and Monday is the first day of the week? In each month, you can find the first Sunday of the month, and the last Monday of the month. They represent two extreme weeks. There is a whole number of weeks in between them. So the number of weeks in that month is 2 plus that whole number. Are you trying to make output similar to [Unix `cal` examples](https://en.wikipedia.org/wiki/Cal_(Unix)#Examples)? – Jeppe Stig Nielsen Dec 23 '19 at 16:49
  • if you can't wrap your head around the math, use a nested case statement. day of week of day 1 and the length of the month control everything. the only one that can ever be only four is february, and it has to start on the first day of the week on a non-leap year to do that. The rest are either 5 unless the length is 30 and the start day of the week is day 7 or the length is 31 and the start day is 6 or 7. In that case the week count is six. easy. – John Lord Dec 23 '19 at 19:04

1 Answers1

0

An advent-of-code style solution:

Enumerable
    .Range(1,12)
    .Select(month => new DateTime(2020, month, 01).AddMonths(1).AddDays(-1))
    .Select(d => Math.Ceiling((d.Day + (int)(new DateTime(d.Year, d.Month, 1).DayOfWeek)) / 7.0))
    .ToArray();

Basically, get the number of days in each month, offset by the day of the week of the first day, take the ceiling of dividing that by 7 to get the number of whole weeks needed for the month.

The total number of weeks counted will be greater than 52, because months share weeks.

brnlmrry
  • 399
  • 2
  • 7
  • A single-line LINQ statement without a lot of context, hardcoded/not reusable, but solves the stated problem. Breaks entirely when the requirements change. – brnlmrry Dec 26 '19 at 16:30