Convert date to Javascript

0

As I can show the date in a DataTable in asp.net, this is my result but I want to show the date

this is my controller

 public ActionResult loadData()
    {
        using (SISTEMAUSGEntities dc = new SISTEMAUSGEntities())
        {
            dc.Configuration.ProxyCreationEnabled = false;
            var data = dc.PROYECTOS.OrderBy(a => a.NAMEPROJECT).ToList();
            return Json(new { data = data }, JsonRequestBehavior.AllowGet);
        }
    }

this is my model

public partial class PROYECTOS
{
    public PROYECTOS()
    {
        this.ENTRYPROJECT = new HashSet<ENTRYPROJECT>();
        this.MATERIALOUTPUT = new HashSet<MATERIALOUTPUT>();
    }

    public int IDPROJECT { get; set; }
    public string NAMEPROJECT { get; set; }
    public string NUMPART { get; set; }
    public string TIPOMATE { get; set; }
    public string PAIS { get; set; }
    public string NUMFACTURA { get; set; }
    public double PESOPALLET { get; set; }
    public double NUMPALLET { get; set; }
    public System.DateTime FECHA { get; set; }
    public string STATUSPROJECT { get; set; }

    public virtual ICollection<ENTRYPROJECT> ENTRYPROJECT { get; set; }
    public virtual ICollection<MATERIALOUTPUT> MATERIALOUTPUT { get; set; }
}

}  and this is my view

<table id="myTable" class="table-responsive" style="width:100%">
        <thead>
            <tr>
                <th>NombreProyecto</th>
                <th>NumParte</th>
                <th>TipoMaterial</th>
                <th>Pais</th>
                <th>NumFactura</th>
                <th>PesoPallet</th>
                <th>NumPallet</th>
                <th>Fecha</th>
                <th>EstadoProyecto</th>
                <th>Acciones</th>
            </tr>
        </thead>
    </table>

 <script>
    $(document).ready(function () {
        $('#myTable').DataTable({
            "ajax": {
                "url": "loadData",
                "type": "GET",
                "datatype": "json"
            }, "columns": [
                { "data": "NAMEPROJECT" },
                { "data": "NUMPART" },
                { "data": "TIPOMATE" },
                { "data": "PAIS" },
                { "data": "NUMFACTURA" },
                { "data": "PESOPALLET" },
                { "data": "NUMPALLET" },
                { "data": "FECHA" },
                { "data": "STATUSPROJECT" },
                {
                    "data": "IDPROJECT", "render": function (data) {
                        return "<a class='btn btn-default btn-sm' @*onclick=PopupForm('@Url.Action("AddOrEdit","Project")*@/" + data + "')><i class='fa fa-pencil'></i> Edit</a><a class='btn btn-danger btn-sm' style='margin-left:5px' onclick=Delete("+data+")><i class='fa fa-trash'></i> Delete</a>";
                },
                    "orderable": false,
                    "searchable": false,
                    "width": "150px"
                }
            ],
            "language": {

                "emptyTable": "No data found, Please click on <b>Add New</b> Button"
            }
        });
    });
</script>
    
asked by Daniel 19.12.2017 в 00:08
source

1 answer

1
  

When you share or work with dates in JSON you should consider in ISO format.

For your problem, you would only need to capture that value and shape it.

$(document).ready(function() {
    $('#example').DataTable( {
        "columnDefs": [
            {
                "render": function ( data, type, row ) {
                    var fecha = new Date(parseInt(data.match(/\d+/)[0]));                
                    return fecha.getMonth() + 1 + '/' + fecha.getDate() + '/' + fecha.getFullYear();;
                },
                "targets": 0
            }
        ]
    } );
} );
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>

<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
      <tr>
          <th>Date</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>/Date(1289376000000)/</td>
      </tr>
      <tr>
          <td>/Date(1286694000000)/</td>
      </tr>
  </tbody>            
</table>

Reference:

answered by 19.12.2017 / 01:23
source