How can I show the next 7 Days (Agenda) C #

1

Live, good night

I'm working on a C # solution, in which I have to develop a schedule and I plan to show the events (if any) at a certain point, as well as show the following 7 days, even if they do not have marked events. >

I developed a method, which given a certain date (int dia, int mes, int ano) actually returns me every day of the indicated month but in fact I need something that returns me for example:

If we are 1 of some month - The program must return from day 1 to day 7

If we are for example a 10 - The program should return from 10 to 17

Therefore always a sequence of 7 Days. Here is the method I have at the moment:

        public string ShowWeekendEvents(int Day, int Month, int Year)
        {


            string aux = "";
            for (int i=0; i<totalEventos; i++)
            {
                if(ev[i].DayMonthYear.Day>=Day&&ev[i].DayMonthYear.Month==Month&&ev[i].DayMonthYear.Year==Year)
                {
                        Console.WriteLine("----------------------------------------------------------");
                        Console.WriteLine("{0}-{1}-{2} {3}-{4} ", ev[i].DayMonthYear.Day, ev[i].DayMonthYear.Month, ev[i].DayMonthYear.Year, ev[i].HoraInicial, ev[i].MinInicial);
                        Console.WriteLine();
                        aux = "Evento: " + ev[i].Events.ToString() + "\n";
                        Console.WriteLine("Data de inicio: {0}/{1}/{2}", ev[i].DayMonthYear.Day, ev[i].DayMonthYear.Month, ev[i].DayMonthYear.Year);
                        Console.WriteLine();
                        Console.WriteLine("Data Final: {0}/{1}/{2}", ev[i].FinalDayMonthYear.Day, ev[i].FinalDayMonthYear.Month, ev[i].FinalDayMonthYear.Year);
                        aux += "Hora Inicio:" + ev[i].HoraInicial.ToString() + "h " + ev[i].MinInicial.ToString() + "Min" + "\n";
                        Console.WriteLine();
                        aux += "Hora Final:" + ev[i].HoraFinal.ToString() + "h " + ev[i].MinFinal.ToString() + "Min   \n";
                        Console.WriteLine(aux);
                        Console.WriteLine("-----------------------------------------------------------");




                        if (ev[i].DayMonthYear.Day >= Day)
                            if (ev[i].DayMonthYear.Day > 7)
                                break;
                            else if (Day == 8 && ev[i].DayMonthYear.Day > 13)
                                break;
                   }
                    //during 7 days


                }
                /*    
                else if (ev[i].DayMonthYear.Day != Day && ev[i].DayMonthYear.Month == Month && ev[i].DayMonthYear.Year == Year)
                {

                    while (j < week-1)
                    {
                        Console.WriteLine("----------------------------------------------------------");
                        Console.WriteLine("{0}-{1}-{2} {3}-{4} ", ev[i].DayMonthYear.Day, ev[i].DayMonthYear.Month, ev[i].DayMonthYear.Year, ev[i].HoraInicial, ev[i].MinInicial);
                        Console.WriteLine();
                        aux = "Evento: " + ev[i].Events.ToString() + "\n";
                        Console.WriteLine("Data de inicio: {0}/{1}/{2}", ev[i].DayMonthYear.Day, ev[i].DayMonthYear.Month, ev[i].DayMonthYear.Year);
                        Console.WriteLine();
                        Console.WriteLine("Data Final: {0}/{1}/{2}", ev[i].FinalDayMonthYear.Day, ev[i].FinalDayMonthYear.Month, ev[i].FinalDayMonthYear.Year);
                        aux += "Hora Inicio:" + ev[i].HoraInicial.ToString() + "h " + ev[i].MinInicial.ToString() + "Min" + "\n";
                        Console.WriteLine();
                        aux += "Hora Final:" + ev[i].HoraFinal.ToString() + "h " + ev[i].MinFinal.ToString() + "Min   \n";
                        Console.WriteLine(aux);
                        Console.WriteLine("-------------------------------------------");



                    }
                }
                 * */

        return aux;
}

However I am quite confused and I am not getting the program to pass to the user what is actually intended. Can you help me rephrase the code?

Thank you in advance.

    
asked by anonymous 01.02.2015 / 00:26

1 answer

1

Create a static class

static class Intervalo
{
    public static IEnumerable<DateTime> IntervaloDias(this DateTime data, int dias)
    {
        return Enumerable.Range(0, (data.AddDays(dias) - data).Days).Select(d => data.AddDays(d));
    }
}

To get the 7 day break

var intervalo = new DateTime(2015, 1, 31).IntervaloDias(7);
foreach (var datas in intervalo)
{
    Console.WriteLine(datas);
}
Console.ReadKey();
//intervalo.ToList() retorna como uma lista de datas
  

Result:

Useyourimagination,goodluck.@vitor-ferreira

Source: link

    
01.02.2015 / 06:13