This is how to use DT to convert Unix Time

This is how to use DT to convert Unix Time

SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
edited November 2015 in Free community support
Working code/fiddle at bottom

My impression is that DataTables would be able to parse the unix epoch time; i found that with the moment-datetime.js plugin for DT and moment.js it is possible and rather simple.

Working Fiddle attached.

Still trying to figure out a better way to format the output, But this at least works and can be formatted somewhat.

This question has an accepted answers - jump to answer

Answers

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    /removed junk code/

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    /removed/

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    /removed junk code/

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    /removed/ bad idea.

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    Finally got it working with Moment.

    <html>
    <head>
    <script type="text/javascript" src="https://cdn.datatables.net/s/dt/jq-2.1.4,dt-1.10.10/datatables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/s/ju/dt-1.10.10/datatables.min.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
    <script src="https://cdn.datatables.net/plug-ins/1.10.10/sorting/datetime-moment.js"></script>
     </head>
     <body>
    <script>       
    $(document).ready(function() {
        $('#deaths').DataTable( {
            "ajax": {
                url: "http://gaming.adduce.co.nz:8888/getPlayerKillsHistory",
                cache: true,
                data: { ids: '76561198061378579' },
                dataSrc: 'PlayersKillsHistory.0.76561198061378579.deaths'
            },
            searching:false,
            paging:false,
            responsive:true,
            columnDefs: [
                           {"type":"html","targets":0},
                           {"type":"unix","targets":1,"render": function (data, type, full, meta) {return moment.utc(data, "x").toISOString().replace(/T/, ' Time: ').replace(/Z/, '');}},
                           {"type":"html","targets":2}
                          ],
            columns:        [{ "title": "Killer Steam",
                               "data": "idKiller"
                                },
                                {"title": "Date/Time", 
                                 "data": "dateTime",
                                 "type": "date"
                                },
                                { "title": "Killer Name",
                                 "data": "killerName"
                                }]
      
        });
    });
    
    </script>
      
    <table id="deaths" class="display" width="100%"></table>
    
    </body>
    </html>
    

    Save as HTML or view this JSFIDDLE

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    Answer ✓

    Perfect - using column.render to change the input data from one format to another on output is the way to do any transform in DataTables.

    Allan

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    Just quickly while your here, How do i get rid of the default width of the table it seems to be hard coded as 1098px and its really screwing up responsive.

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2

    Nevermind :P I found

     bAutoWidth:false
    
    
This discussion has been closed.