12

I want to find a date are now on week number with c# desktop Application.

I've been looking on google, but none that fit my needs.

How do I get a week in a month as the example below?

Example:

I want January 6, 2014 = the first week of January

January 30, 2014 = fourth week of January

but 1 February 2014 = week 4 in January

and 3 February 2014 was the first week in February

Enkhay
  • 232
  • 2
  • 4
  • 16

3 Answers3

22

Here is the method:

static int GetWeekNumberOfMonth(DateTime date)
{
    date = date.Date;
    DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
    DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    if (firstMonthMonday > date)
    {
        firstMonthDay = firstMonthDay.AddMonths(-1);
        firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    }
    return (date - firstMonthMonday).Days / 7 + 1;
}

Test:

Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 1, 6)));  // 1
Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 1, 30))); // 4
Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 2, 1)));  // 4
Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 2, 3)));  // 1
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
  • Absolute, this is the code what i want. Thank you very much @Ulugbek – Enkhay Apr 14 '14 at 12:57
  • 2
    This does not working for 21 March 2021 – Tony Apr 03 '21 at 08:44
  • The code returns 4 for then following date: Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2022,4,1))); – Mahmoud Amini Arjmand May 19 '22 at 13:32
  • 1
    @MahmoodAbolfathzadeh the 4th week for 2022.04.01 is correct according to description provided by TS (one of samples is that 2022.02.01 is a 4th week of January). March 1st week - 2022.03.07-2022.03.13 March 2nd week - 2022.03.14-2022.03.20 March 3rd week - 2022.03.21-2022.03.27 March 4th week - 2022.03.28-2022.04.03 So April 1st falls into 4th week of March. – Ulugbek Umirov May 27 '22 at 11:41
4
  public static int GetWeekNumber(DateTime dt)
  {
          CultureInfo curr= CultureInfo.CurrentCulture;
          int week = curr.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
          return week;
  }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 3
    He wants the week number of month, not of year. – Ulugbek Umirov Apr 14 '14 at 12:34
  • Interesting piece of code, but this will return the week of -year-, not month. Also, beware of "FirstFourDayWeek" which may give unattended result. – AFract Apr 14 '14 at 12:35
  • the result is 16 if i use datetime.now, i want like my example get week on month. I want January 6, 2014 = the first week of January. January 30, 2014 = fourth week of January. but 1 February 2014 = week 4 in January. and 3 February 2014 was the first week in February. – Enkhay Apr 14 '14 at 12:36
  • @Enkhay Yea, got your question wrong, may be you can add some code to return the week of the month, probably will get downvotes now – Sajeetharan Apr 14 '14 at 12:39
  • Dim WeekNumber As Integer = GetWeekNumber(MyDate) - GetWeekNumber(LastDayLastMonth) – masteroleary Oct 01 '15 at 17:19
1

I think this is what you want:

public static int GetWeekOfMonth(DateTime date)  
{  
    DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);  

    while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)  
        date = date.AddDays(1);  

    return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays  / 7f) + 1;  
} 

Its authored by David M Morton on http://social.msdn.microsoft.com/Forums/vstudio/en-US/bf504bba-85cb-492d-a8f7-4ccabdf882cb/get-week-number-for-month

rosko
  • 464
  • 3
  • 16
  • This code calculates the number of weeks regardless of the date of the first was on Monday or not. What I want is the first Monday of each month is the first week of the month. For example, if January 1, not on Monday, meaning January 1, including last week in the previous month. – Enkhay Apr 14 '14 at 12:54