Trouble with converting datetime from WebApi

Trouble with converting datetime from WebApi

Mattias83Mattias83 Posts: 2Questions: 0Answers: 0

Good day all

I have some trouble with getting a correct datetime format from data coming from a WebApi that i want to use in my table.

the data looks like this:

{
     "data": [
         {
             "Pfid": 61000,
             "Typ": "D",
             "Pplats": "636-05-B",
             "Artnr": "VSV182840",
             "Artben": "Galler Spirit 800C KPL",
             "Saldo": 3,
             "Saldotyp": "ST",
             "Saldoochtyp": "3 ST",
             "Pgrp_nr": 1035,
             "Ordernr": "116595",
             "Rappnr": 23382114,
             "Trp": "646",
             "Artrev": "1",
             "Pkg": 1,
             "Pkg_total": 1,
             "Reported_by": "Lillo Italo Valdivia",
             "Datum": "2018-12-04T05:48:14.183",
             "Npgrp_nr": 0,
             "Op": 0,
             "Nop": 0,
             "Flyttad": "2018-12-04T13:23:33.33"
         }
    ]
 }

and this is my datatable config using moment and datetime plug-in

<script src="/DataTables/datatables.min.js"></script>
<script src="/DataTables/datetime/moment.js"></script>
<script src="/DataTables/datetime/datetime.js"></script>


    <script type="text/javascript">
        $(document).ready(function () {    
            $('#example').DataTable({
                ajax: {
                    "url": '/api/MaMaDB/GetPfInfo?user=test',
                    "type": "GET"
                },                
                columns: [
                    { data: "artnr" },
                    { data: "artben" },
                    { data: "saldo" },
                    { data: "saldotyp" },
                    { data: "pgrp_nr" },
                    { data: "ordernr" },
                    { data: "rappnr" },
                    { data: "trp" },
                    { data: "reported_by" },
                    { data: "datum" },
                ],
                columnDefs: [
                    { targets: 9, render: $.fn.dataTable.render.moment('YYYY-MM-DD') }
                ]
            });
        });

    </script>

but i only get invalid date in the date column and not the date format that i want

if i dont use datetime plugin datatables will just output the date as 2018-12-04T05:48:14.183

Any idea how i need to modify the settings for it to recognize the dates in the data correctly?

Best Regards
Mattias

Replies

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    Hi @Mattias83 ,

    You need to manipulate that date, as the format needs converting. See here, this is using one of your date formats,

    Cheers,

    Colin

  • Mattias83Mattias83 Posts: 2Questions: 0Answers: 0

    that worked perfectly, many thanks :)

This discussion has been closed.