0

I am currently trying to get the week number of the first day of a given month and year in C#, but I didn't find any solution, HELP!

Example : if I give it month :02 and year : 2021 it will return the week number of the first day of february which is : Week 05
If I give it month :01 and year : 2021 it will return the week number of the 1st day of January which is : Week 53

  • The trick is that the first day of a month is always 01. So you can create a date time DateTime (int year, int month, int day);` and use the Calendar to get the Week of the year https://learn.microsoft.com/en-us/dotnet/api/system.globalization.calendar.getweekofyear?view=net-5.0 – Drag and Drop Feb 19 '21 at 13:48
  • Define "week number", do you mean according to .NET? Or some other set of rules? – Lasse V. Karlsen Feb 19 '21 at 13:48
  • But week number can be a trick question depending of calendar and iso you ask https://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date – Drag and Drop Feb 19 '21 at 13:50
  • 1
    Does this answer your question? [Get the correct week number of a given date](https://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date) – Drag and Drop Feb 19 '21 at 13:50
  • Thank you for your return, I have found the solution which is : var firstDayOfMonth = new DateTime(yearr, semainef, 1); CultureInfo ciCurr = CultureInfo.CurrentCulture; int weekNum = ciCurr.Calendar.GetWeekOfYear(firstDayOfMonth, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); – minguo binguo Feb 19 '21 at 14:03

1 Answers1

0

Ok so you have the year (lets say 1984) and you have a month (lets say june). The date we are interested in is therefore 1-6-1984. We can construct this date like this:

DateTime d = new DateTime(1984, 6, 1);

We also need to have a CultureInfo because the concept of "weeks" can differ all around the world

CultureInfo myCI = new CultureInfo("en-US");
Calender myCal = myCI.Calendar;

We can now see what day of the week this is by doing this:

int week = GetWeekOfYear(d, myCI.DateTimeFormat.CalendarWeekRule, myCI.DateTimeFormat.FirstDayOfWeek;);

Therefore we can make a method for this like this:

public int getDayOfWeekFromMonthAndYear(int month, int year) {
    DateTime d = new DateTime(1984, 6, 1);
    CultureInfo myCI = new CultureInfo("en-US");
    int week = myCal.GetWeekOfYear(d, myCI.DateTimeFormat.CalendarWeekRule, myCI.DateTimeFormat.FirstDayOfWeek;);
}
Luke_
  • 745
  • 10
  • 23
  • The question is how to get the **week number**, not the day of the week. – stuartd Feb 19 '21 at 13:58
  • 1
    Also the order of the parameters for the DateTime constructor is incorrect. June did not have 1984 days in year 1. – Lasse V. Karlsen Feb 19 '21 at 14:00
  • Thank you for your return, I have found the solution which is : var firstDayOfMonth = new DateTime(yearr, semainef, 1); CultureInfo ciCurr = CultureInfo.CurrentCulture; int weekNum = ciCurr.Calendar.GetWeekOfYear(firstDayOfMonth, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); – minguo binguo Feb 19 '21 at 14:03
  • @stuartd i fixed my answer, it should now be working as expected – Luke_ Feb 19 '21 at 14:05