Format date and time and list according to current date

Asked

Viewed 844 times

5

This application has two tables: Students and Occurrences. To show the occurrences I can calmly... Only now I’ve come up with a new business rule: show occurrences by date. So basically it would be this, I would have to show the most current occurrences according to the date, ie, if for example I generated an occurrence yesterday, then it will show it in evidence above the others, but if I generate one today, this one today will have to be in evidence. For example, what happens here in Sopt. How could I use c# native formatting to display the date and time/minutes. That is, I wish I could show beyond the date, the hour and the minutes, because I need this data so that at the time of listing.

And how could I make this listing by putting the current one in view of the user, that is, in evidence ?

Here I will show how is the search I do in the bank to show the occurrences:

The Actionresult that scans the table and shows the data (which for now, I show the names, but I want to show the names and dates as described above):

public ActionResult Inicio()
{
    List<Ocorrencia> resultado = db.Ocorrencias.Include(o => o.Aluno).ToList();
    return View(resultado);
}

So basically I just look for the names, and with that the new occurrences are getting below the old ones, and that’s not what I need. I need the new ones, listed by date, stand up, in evidence.... But how do I do that ?

  • What you want to do is: sort the occurrences by date, and in the view display the date in the format dd-MM-yyyy HH:mm right?

  • Exactly that ! It is because the way it is, it lists all, and the order is: the new ones are under the old ones...

  • @Have you noticed that all your posts need to be edited by other users? Take a look at how it’s getting after editing that people do to see how to use the editor correctly. What you get confused about.

1 answer

5


If I understand correctly, you want to list all your occurrences by date. If so, simply in the query made in the controller, sort by date:

public ActionResult Inicio()
{
    List<Ocorrencia> resultado = db.Ocorrencias.Include(o => o.Aluno).OrderBy(c => c.DataAOrdenar).ToList();
    return View(resultado);
}

To display the date in the format dd-MM-yyyy HH:mm, in-view:

@Html.TextBoxFor(model => model.DataAOrdenar, "{0:dd/MM/yyyy HH:mm}")
  • Wow, too GOOD and too simple ! And I think I have to change everything ! Just one question, as I haven’t put the date on the model yet, I could put a Datetime to date without using the Annotations native to the c# ?

  • 1

    Yes, I’m assuming you have a Datetime on the model. If your field is Datetime you don’t need to do anything other than what I put here

  • Man that great, is because it has the Annotations c#, like Required, Display, Stringformat. Since I don’t have that attribute in the model yet, and how I’m going to create it will be a Datetime, so I don’t have to format it in the model but in the View... Right ?

  • 1

    Right. Just define how public DateTime DataAOrdenar { get; set; }. The format is always manipulated in the view or controller, so you don’t need to make any string format

  • Man, thank you very much ! Really ! When you get home I see it right. And if you put error here !

  • It worked ! Thank you very much !

Show 1 more comment

Browser other questions tagged

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