-2

I have an exercise with C# where I have to enter the day and the month for example 6.4 an then calculate the Calenderweek. So I searched on the internet but I found nothing. And also that is easier the year starts at a monday.

Console.WriteLine("Bitte Tag eingeben");
int day = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Bitte Monat eingeben");
int month = Convert.ToInt32(Console.ReadLine());

calender(day, month);
Joehl
  • 3,671
  • 3
  • 25
  • 53
  • 3
    What do you mean by "Calendar Week"? Are you trying to calculate the week number within the year, such as week 30 out of 52? Either way, you should probably be using DateTime (https://learn.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8). You can also take a look at Calendar.GetWeekOfYear to see if this that would be of any use: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.calendar.getweekofyear?view=netframework-4.8 – VirtualValentin Nov 22 '19 at 09:00
  • Is it a leap year? If you really _really_ have to calculate it by hand (you say it's an excercise, I'd argue it's a bad/unrealistic exercise): you know how many days there are in a month (that's why it's important to know if february has 28 or 29 days). So add all the days of all the months _before_ the given month, add the given day value and divide by number of days per week to get the week number. – Corak Nov 22 '19 at 09:12
  • So you enter a day for example the 6 and then the month 3 so and then it calculate the week of the year for that date and the year start on a mondy and feb has 29 days –  Nov 22 '19 at 09:15
  • Dupe target => https://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date – xdtTransform Nov 22 '19 at 09:40

1 Answers1

1

The simplest way would be to use Calendar.GetWeekOfYear, athough this would require having the year input as well:

Console.WriteLine("Bitte Tag eingeben");
int day = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Bitte Monat eingeben");
int month = Convert.ToInt32(Console.ReadLine());

DateTime dt = new DateTime(2019, month, day);

CultureInfo culture = CultureInfo.CurrentCulture;
CalendarWeekRule cwr = culture.DateTimeFormat.CalendarWeekRule;
DayOfWeek dow = culture.DateTimeFormat.FirstDayOfWeek;

int weekOfYear = culture.Calendar.GetWeekOfYear(dt, cwr, dow);

Having said this it wouldn't be too hard to construct a manual calculation with the absence of the year (with the assumption it isn't a leap year):

// Construct an array, with month as index and days as value
int[] monthDays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// Count the number of days up to the given month
int days = 0;
for (int i = 0; i < month - 1; i++)
{
    days += monthDays[i];
}

// Add the given number of days
days += day

// Divide by 7 to get the week of the year
int weekOfYear = (int)Math.Ceiling((double)days / 7);

Of course, if it is a leap year you will need to amend the array to have new int[] { 31, 29, ....

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • Week is not about total number of day divided by 7. There is 52 week in a years but 52*7 = 364... Depending on the first day of the year the last day of the same years may be the first day of the 54th week. – xdtTransform Nov 22 '19 at 09:38
  • 1
    The question already makes assumptions (year starts with Monday; per comment: february has 29 days; implied by german text: week also starts on monday)... of course this isn't "production" code (that would be closer to the example using `Calendar.GetWeekOfYear`), but as an exercise with those assumptions it's alright. – Corak Nov 22 '19 at 10:00
  • @Corak, For german iso Date production ready code may use https://learn.microsoft.com/en-us/dotnet/api/system.globalization.isoweek?view=netcore-3.0. It's a bit better than GetWeekOfYear. While I understand your point I do not reduce answers target audience to OP only. If it's about Op specific case then We should use year 2018/2007/2001 as those were not leap and start on a monday. – xdtTransform Nov 22 '19 at 10:34