Format data dd/mm/yyyy in a jquery Datatable.net

Asked

Viewed 881 times

1

How I format that date 2016-10-23T20:30:01.017 in one Datatable.net in the Brazilian format dd/mm/yyyy ?

Controller:

This relevant snippet of the code is where I populate the object that is in the ok format: dd/mm/yyyyyy hh:mm:ss:

List<Cliente> lTotalClienteAux = _IRepositorio.ListarCliente().ToList();

                List<Cliente> lTotalCliente = new List<Cliente>();
                foreach (var item in lTotalClienteAux)
                {
                    Cliente oCliente = new Cliente();
                    oCliente.ClienteID = item.ClienteID;
                    oCliente.DataCadastro = item.DataCadastro;                       
                    lTotalCliente.Add(oCliente);
                }

View When you click here in the View the format changes to: 2016-10-24T20:35:13.617

{
      "mRender": function (data, type, full) {
      var dt = full['DataCadastro'];
      return '<td>' + dt + '</td>';
     }
},

web config. On the web.config I have this tag indicating the culture:

<system.web>
    <globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" />

2 answers

2


You can do it that way.

var data = new Date("2016-10-23T20:30:01.017");
var dia = data.getDate();
var mes = data.getMonth();
var ano = data.getFullYear();

document.write(dia + '/' + mes + '/' + ano);

Return

23/10/2016
  • Your solution even works and shows the formatted date, but I’ve been searching and accurate tipar this column as type "Datetime` to sort.

1

Serialize the data to Datatable.net already in the correct format instead of using javascript to format the date. Deliver the data already ready.

In this line: oCliente.DataCadastro = item.DataCadastro;

You must do: oCliente.DataCadastro = item.DataCadastro.ToShortDateString();

The Dataregistration property of the Client class must be a string. If Client was a domain object, you must create a view model for your screen, in this case, Clienteviewmodel and in this view model, Datacadastro will be a string.

  • in the controller’s return method Jsonresult is already serealized: {23/10/2016 20:30:01}.

  • So where is the 2016-10-23T20:30:01.017 coming from that you mentioned in the question? Also, if your object being serialized is has a Datetime property, you must return a string instead of Datetime. Ai when building your object, you can put something like Campodate = Datetime.Now.Toshortdatestring(). This Toshortdatestring method already returns in the culture format configured in the application.

  • Or you can do a Datetime.Now.Tostring("dd/MM/yyyy")

  • This Date comes from the Sql Server database I edited the question to improve understanding. When I populate the object in Controller the format is ok, but in View loads in another format, initially in mRender I thought something like return ´'<td>' + dt.ToString("dd/MM/yyyy") + '</td>'; , but it doesn’t work and I’m wondering if it’s some configuration in itself Datatable.net

Browser other questions tagged

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