How do I find the week number in a month

I need to find week number in a month. It could be 4 or 5 weeks in a month (depending on the calendar) and I need to find the week number based on the current date. Note : (Day)/7 would not work since Week 1 or Week 4 would be assumed to have 7 days which is not always the case.

2 answers

  • answered 2019-08-13 03:46 Tiger Liu

    Set the count to 1, and enumerate every day of a month. Add to count by 1 for each sunday except the first day. After the enumeration, the count is the number of weeks in that month.

  • answered 2019-08-13 03:48 Saif

    just do like this

    DateTime date =DateTime.Now;
    int weekOfMonth=(date.Day + ((int)date.DayOfWeek)) / 7 + 1;

    calculation based on Malaysia time is (today number is 13 + day of week is 2) divided by 7 and then add one to result so result would be 3