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

Asked

Viewed 626 times

1

Hail, good night

I am working on a C#solution, where I have to develop an agenda and at some point I intend to show the events (if any) as well as show the following 7 days, even if they are not marked.

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

If we are 1 month away - The program should return from day 1 until day 7

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

So always a sequence of 7 Days. Here’s 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. I wonder if you could help me rewrite the code?

Thanks in advance.

  • http://answall.com/help/mcve

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 take 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

Upshot:

Resultado

Use your imagination, good luck. @Vitor-Ferreira

Source: https://stackoverflow.com/questions/3738748/create-an-array-or-list-of-all-dates-between-two-dates

Browser other questions tagged

You are not signed in. Login or sign up in order to post.