Create a table with dates

Asked

Viewed 177 times

4

I am creating a table and I need the columns to be created as follows: Mon, 23 Nov | Tue,24 Nov | Wed,25 Nov | Thu,26 Nov | Fri,27 Nov | Sat,28 Nov | Sun, 29 Nov

To create the Table I pass a date parameter (which is selected through a calendar)

I’m doing so to create the Table:

    DateTime diaAgenda = 'Data que foi selecionada no calendario';

    public DataTable gerarAgenda(DateTime dia)
    {
        DataTable tabela = new DataTable();

        string diahoje = String.Format("{0:ddd, MMM d, yyyy}", dia);

        tabela.Columns.Add("" + diahoje);

        for (int data = 1; data < 7; data++)
        {
            dia = dia.AddDays(1);
            diahoje = String.Format("{0:ddd, MMM d, yyyy}", dia);
            tabela.Columns.Add("" + diahoje);
        }
        return tabela;            
    }

Making this way the table I Gero gets the following columns:

Thu,26 Nov | Fri,27 Nov | Sat,28 Nov | Sun, 29 Nov | Mon, 30 Nov | Tue,01 Dec | Wed,02 Dec |

How could I do to create the columns like this:

Mon, 23 Nov | Tue,24 Nov | Wed,25 Nov | Thu,26 Nov | Fri,27 Nov | Sat,28 Nov | Sun, 29 Nov

  • 1

    Your question is very confusing. Why can’t you make the first column second?

  • because I don’t know how to set that week always start on Monday..

  • 1

    What is the parameter for dia? You are generating a table based on it?

  • 1

    Bad question generates bad answers :)

  • 1

    Really @bigown. You saw that switch-case in the second answer? Is it the case to vote to close as is not clear enough?

  • @jbueno the parameter I pass the day..

  • This I noticed by its name. The question is: what is the parameter for?

  • I edited the question. It became clearer?

Show 3 more comments

3 answers

4


If I understood your problem then I would say it is simply the parameter dia that you are going through. You are not going through it by marking a Monday, so the "mistake" occurs of not starting on Monday as you wish.

public DataTable gerarAgenda(DateTime dia)
{
    switch(dia.DayOfWeek)
    {
        case DayOfWeek.Tuesday: dia.AddDays(-1); break;
        case DayOfWeek.Wednesday: dia.AddDays(-2); break;
        case DayOfWeek.Thursday: dia.AddDays(-3); break;
        case DayOfWeek.Friday: dia.AddDays(-4); break;
        case DayOfWeek.Saturday: dia.AddDays(-5); break;
        case DayOfWeek.Sunday: dia.AddDays(-6); break;
    }

    DataTable tabela = new DataTable();

    string diahoje = String.Format("{0:ddd, MMM d, yyyy}", dia);

    tabela.Columns.Add("" + diahoje);

    for (int data = 1; data < 7; data++)
    {
        dia = dia.AddDays(1);
        diahoje = String.Format("{0:ddd, MMM d, yyyy}", dia);
        tabela.Columns.Add("" + diahoje);
    }
    return tabela;            
}
  • 1

    How do I mark for the day = Monday?

2

You can check if it’s Monday that way..

if(dia == DayOfWeek.Saturday)
  • 3

    Saturday? '-'

  • @jbueno ...damned... I won’t even edit to get your two points uteis shuahs .... Monday .... if(dia == DayOfWeek.Monday )

  • 1

    But in the end, I was the first to tip the rest was a copy

2

Ah! You want the first column to be MONDAY, is that it? Forehead:

var diaDaSemana = (int) dia.DayOfWeek;
var inicioDaSemana = dia.AddDays(diaDaSemana-1);
for (int dias = 1; dias < 7; dias++)
{
    data = inicioDaSemana.AddDays(dias);
    dataFormatada = String.Format("{0:ddd, MMM d, yyyy}", data);
    tabela.Columns.Add("" + dataFormatada);
}

But taking advantage of the cue, it seems very unproductive - or even inappropriate - what you’re trying to do.

I recommend doing a refactoring on this code. It is very strange the end result of it.

Browser other questions tagged

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