Format Date Column

Format Date Column

guicamarottoguicamarotto Posts: 5Questions: 2Answers: 0
edited May 2020 in Free community support

I want to format a column of date type. I tried use moment.js and datatables datetime render, but don't work.

Tried also the code below. Don't work too.

{
                            "targets": [3],
                            "sType": "date-ddmmyyyy"//<--- this one
                        }

My data is coming like this 2020-05-06T17:16:34.933 but I need something like that 06/05/202 (like DD/MM/YYYY)

Anyone knows a solution for that?

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I tried use moment.js and datatables datetime render, but don't work.

    That combination works perfectly well if it's used correctly.
    Please show all your code.

  • guicamarottoguicamarotto Posts: 5Questions: 2Answers: 0

    without format

    error using that way

    script section

    <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.19/dataRender/datetime.js"></script>
    
    datatable = $('#tblFiles')
                    .DataTable({
                        "paging": true,
                        "searching": true,
                        "ordering": true,
                        "ajax": {
                            "url": loadUrl,
                            "type": "GET",
                            "dataType": "json"
                        },
                        "columnDefs": [
                            {
                                "targets": [0],
                                "visible": false,
                                "searchable": false
                            },
                            {
                                "targets": [3],
                                "render": $.fn.dataTable.moment('DD/MM/YYYY')
                            }
                        ],
                        "columns": [
                            { "data": "fileId" },
                            { "data": "fileName" },
                            { "data": "fileSource" },
                            { "data": "createdOn" },
                            { "data": "status.statusName" },
                        "language": {
                            "emptyTable": "No files found",
                            "processing": '<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i><span class="sr-only">Loading...</span> '
                        },
                        "processing": true
    
                    });
    
            });
    

    If I change render to this, the error changes

    {
       "targets": [3],
        render: $.fn.dataTable.render.moment('Do MMM YYYYY')
    }
    

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Have you tried loading moment.js before dataTables?

  • kthorngrenkthorngren Posts: 21,222Questions: 26Answers: 4,929

    There are lots of threads on the forum with that error. See if this one or this helps. If not please post a link to your page or a test case so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • guicamarottoguicamarotto Posts: 5Questions: 2Answers: 0

    I have changed my approach guys, once I using .net mvc, I created a new property on my model to pass the formated date to my view. Thanks anyway.

This discussion has been closed.