-1

I am currently trying to calculate the weeknumber using the dayOfYear

string dateStr = $"{Model.Date:dddd, d MMMM}";
Date.Text = char.ToUpper(dateStr[0]) + dateStr.Substring(1);

WeekOfYear.Text = $"Week {Model.Date.DayOfYear / 7}";

The only thing is this sometimes gives back the wrong weeknumber because it gives back a double and not a weeknumber.

Is there a better way to calculate this?

DerStarkeBaer
  • 669
  • 8
  • 28
GoozMeister
  • 334
  • 2
  • 17
  • Look for modulo divison – Reporter Nov 21 '19 at 11:11
  • Always use already implemented functionality for things like date and time. There are many special cases to care about. I.e. a week is assigned to the year its thursday(?) belongs to. So the 01.01 is still the last week of the previous year if it's a saturday. – Chrᴉz remembers Monica Nov 21 '19 at 14:44

1 Answers1

0

I hope this helps:

int numeroSemana = ObterNumeroSemana(Convert.ToDateTime(visita["dataPrevistaVisita"].ToString()));

public static int ObterNumeroSemana(DateTime dataVisita)
{
  CultureInfo ciCurr = CultureInfo.CurrentCulture;
  int weekNum = ciCurr.Calendar.GetWeekOfYear(dataVisita, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
  return weekNum;

}

If you have questions let me know. Cheers.

Lafuente
  • 146
  • 2
  • 5