I ended up finding a solution, perhaps not the most effective. I built from the @Raulalmeira answer.
var listaDatas = new List<DateTime>(); //Lista que vai receber as datas
1º- I created a list for each week of the month
listaDatasPrimeiraSemana = new List<DateTime>();
listaDatasSegundaSemana = new List<DateTime>();
listaDatasTerceiraSemana = new List<DateTime>();
listaDatasQuartaSemana = new List<DateTime>();
listaDatasQuintaSemana = new List<DateTime>();
2º- I started a date starting on the 1st of January
DateTime date = DateTime(2014, 1, 1);
3º- I filled in the lists of weeks, running the five weeks to fill in the lists of weeks
while (numSemana <= 5) //Enquanto a semana for menor de 5 continua a percorrer
{
DateTime tempdate = data.AddDays(-data.Day + 1);
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNumStart = ciCurr.Calendar.GetWeekOfYear(tempdate, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
int weekNum = ciCurr.Calendar.GetWeekOfYear(data, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
numSemana = weekNum - weekNumStart + 1;
if (data.AddDays(1).Month != data.Month) { break; }
if (numSemana == 1)
{
listaDatasPrimeiraSemana.Add(data);
}
if (numSemana == 2)
{
listaDatasSegundaSemana.Add(data);
}
if (numSemana == 3)
{
listaDatasTerceiraSemana.Add(data);
}
if (numSemana == 4)
{
listaDatasQuartaSemana.Add(data);
}
if (numSemana == 5)
{
listaDatasQuintaSemana.Add(data);
}
data = data.AddDays(1);
}
4º- I made the conditions to see which week the user chose
if (servico.SemanaMensalPeriod == "Primeira") //servico.SemanaMensalPeriod contém a semana escolhida pelo utilizador
{
foreach (var item in listaDatasPrimeiraSemana)//Percorro a primeira semana
{
if (item.DayOfWeek == DayOfWeek.Monday) { listaDatas.Add(item); } //Verifico se na semana há alguma segunda feira, e caso exista adiciono à lista de datas
}
}
...
Condição if igual à de cima para verificar se tenho alguma segunda-feira nas restantes semanas
...
ATTENTION
The code above only shows the result for the month of January, to do for a period of time or for the whole year just put the code on top of a cycle for and go iterating the month (that’s how I did, but I didn’t find it necessary to post).
I know that it is by far the most effective solution and I am aware of that, I just leave it here as I ended up solving the problem. Although I’ve seen better solutions than mine
I’m thinking of creating a list of dates for each week in the month. I see where the first day of the month is (in the week) and start generating the lists (a list for each week), from that date. It’ll be the right way?
– CesarMiguel
You already have a very similar question here Make sure it doesn’t suit you?
– Fernando Leal
I even asked that question. It turns out to be different questions, because here I want from 1 to 5 the number of the week in the month, where it starts and where it ends. The other question has no way of knowing which second or third week of a given month
– CesarMiguel
If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.
– Icaro Martins