HOW TO FORMAT DATE TYPE dd / MM / YY

HOW TO FORMAT DATE TYPE dd / MM / YY

mario_618mario_618 Posts: 10Questions: 2Answers: 0

I am using datatable extracting data from an ajax file passing data with json and in the date column it comes out 2015-10-22T00: 00: 00.000000Z and I want it to come out 10/22/2015

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735

    See if the Datetime renderer does what you want.

    Kevin

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0

    kthorngren
    Thank you very much, could you please explain to me what columnDefs and target mean

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735
    edited October 2020

    Please see the docs for columnDefs and columnDefs.targets. If you are using columns you can just add the renderer to the appropriate column without needing to use columnDefs.

    Kevin

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0

    kthorngren
    Thank you very much, I have this:

    .......').dataTable( {
    columns:[
    {data:'ID_XX'},
    {data:'DesXXx'},
    {data:'DATEXXXxx'},
    ],
    columnDefs: [ {
    targets: 2,
    render: $.fn.dataTable.render.moment( 'Do MMM YYYY' )
    } ]
    .......

    BUT it still doesn't work for me, it shows me in the column the message of "Invalid date"

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735

    The datetime.js docs show there is the possibility of using up to 3 parameters. You might need to provide the source format, for example: $.fn.dataTable.render.moment( from, to );

    To learn more see the moment.js docs.

    Kevin

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0

    Thanks Kevin, check the documentation and I found this:
    moment().format(); // 2020-10-26T10:32:56-04:00

    this would be the date that I have to format 2015-10-22T00: 00: 00.000000Z

    then I can't find in the documentation what the source format would be or how I would have to use it .., I did it this way:

    columns:[
    {data:'ID_ActividadSis'},
    {data:'DescActividadSis'},
    {data:'FechActividadSis',
    type: 'date',
    render: $.fn.dataTable.render.moment('YYYY-MM-DDTHH:mm:ssZ','YYYY/MM/DD' )
    },

    BUT it still doesn't work for me :neutral:

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735

    Not sure if you found the moment.js format docs. Looks like you need to add the fractional second of S. Maybe something like this:
    YYYY-MM-DDTHH:mm:ss.SSSSSSZ

    I have verified that format fully matches what you have. If you still need help please build a simple test case so we can help define the moment formats.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0

    I change http://live.datatables.net/decudehu/1/edit
    <tr>
    <td>Ashton Cox</td>
    <td>Technical Author</td>
    <td>San Francisco</td>
    <td>66</td>
    <td>2015-10-22T00:00:00.000000Z</td>
    <td>$4,800</td>
    </tr>
    that row its like me, but not working

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0

    http://live.datatables.net/decudehu/1/edit

    Ashton Cox Technical Author San Francisco 66 2015-10-22T00:00:00.000000Z $4,800

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0
    edited October 2020

    using a test case put all my dates in the table according to the following link
    http://live.datatables.net/decudehu/1/edit
    and I have commented on the way I am trying to format the date

    $(document).ready( function () {
    var table = $('#example').DataTable( {
    // columnDefs: [ {
    // targets: 4,
    // render: $.fn.dataTable.render.moment('YYYY-MM-DDTHH:mm:ss.SSSSSSZ','YYYY/MM/DD' )
    // } ]
    } );
    } );

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0
    edited October 2020

    sorry this is the modified
    http://live.datatables.net/decudehu/1/edit

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735
    Answer ✓

    Can't explain why but it seems to work with using only 4 SSSS instead o 6. For example:
    http://live.datatables.net/luviyuzi/1/edit

    Kevin

  • mario_618mario_618 Posts: 10Questions: 2Answers: 0

    OMG :D ITS WORKING NOW :)

    THANKS THANKS THANKS

This discussion has been closed.