Format date to dd/mm/yyyy using Datattables. Netcore

Asked

Viewed 137 times

0

In my project I’m using the library Datatables for mounting of my tables, the problem is that it is returning the date in the standard american (format I write to the database) and am unable to customize to display in the pattern brazilian DD/MM/YYYY.

I’ve tried using jQuery masks but they change only the first record, the following remain in American format. I tried to use Moment.js but could not implement. Below my codes and a table print.

Custom JS from Datatables

    $(document).ready(function () {
  $("#table").dataTable({

    aLengthMenu: [
      [5, 10, 25, -1],
      [5, 10, 25, "Todos"],
    ],
    iDisplayLength: 5,

   "language": {
        "url": "https://cdn.datatables.net/plug-ins/1.10.20/i18n/Portuguese-Brasil.json"
    }
  });
});

Table:

<div class="jumbotron">
    <div class="card-header">
        <h3 class="display-5">
            <i class="fas fa-ticket-alt fa-lg"></i>
            Tickets Abertos
            <a asp-action="NovoTicket" class="btn btn-primary btn-lg" data-toggle="tooltip" data-placement="right" title="Novo Ticket">
                <i class="fas fa-plus-circle fa-lg"></i>
            </a>
        </h3>
    </div>
    <div class="card-body">
        <table id="table" class="table table-striped table-hover table-responsive-md">
            <thead>
            <tr>
                <th>Id</th>
                <th>Equipamento</th>
                <th>PA</th>
                <th>Problema</th>
                <th>Data Abertura</th>
                <th>Valor</th>
                <th>Status</th>
                <th>Responsável</th>
                <th>Opções</th>
            </tr>
            </thead>
            <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => item.TicketId)</td>
                    <td>@Html.DisplayFor(modelItem => item.Equipamento.TipoDeEquipamento)</td>
                    <td>@Html.DisplayFor(modelItem => item.PA.Nome)</td>
                    <td>@Html.DisplayFor(modelItem => item.Descricao)</td>
                    <td">@Html.DisplayFor(modelItem => item.DataAbertura)</td>
                    <td>R$ @Html.DisplayFor(modelItem => item.Valor)</td>
                    <td>@Html.DisplayFor(modelItem => item.Status)</td>
                    <td>@Html.DisplayFor(modelItem => item.Usuario.Nome)</td>

                    <td>
                        <a asp-action="AtualizarTicket" asp-route-TicketId="@item.TicketId" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="Atualizar Ticket">
                            <i class="far fa-edit"></i>
                        </a>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    </div>
</div>

Table: inserir a descrição da imagem aqui

2 answers

1

You can add a standard culture at your application startup.

public class Program
{
    public static void Main(string[] args)
    {
        // Define a cultura padrão como "pt-BR"
        CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-BR");
        CreateWebHostBuilder(args).Build().Run();
    }
}

Or else you can force the format in Razor if its variable Open date be the type Datetime.

@Html.DisplayFor(modelItem => item.DataAbertura.ToString("dd/MM/yyyy HH:mm"))
  • Using the right culture, put in my project in precise English culture. Already trying to use Tostring in View I received the following error: "Invalidoperationexception: Templates can be used only with field access, Property access, single-Dimension array index, or single-Parameter custom indexer Expressions."

0


I decided to add a Dataannotations formatting the date in the Model:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DataAbertura { get; set; }

DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DataEncerramento { get; set; }

Browser other questions tagged

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