Cannot get moment.js to work...

Cannot get moment.js to work...

FrefiFrefi Posts: 2Questions: 1Answers: 1

Hi!

Should the moment.js plugin still work? Im quite new to programing so im not sure if im doing this right...

I have implemented DataTables which works great (thank you so much for this!) except on thing.
My date time column does not sort in date-order, seems to be sorted as string. Example: "5/10", "5/16", "5/18", "5/3", "5/18", "5/4". The date-time-format in table is "5/11/2018 2:22:22 PM".

I tried to add the pluging for moment.js and datetime-moment.js as described in this blog page ... but it seems that the "date column" is not recognized as date-format... i cannot even change the format.
Obviously I have done something wrong but I do not understand what...

I use it in a MVC .net Core 2.1 projekt and my view xxx.chtml file looks something like this....

    @model IEnumerable<xxx.WebApp.Models.xxx>
    @{ViewData["Title"] = "Index";}
    <table class="display" id="tableDataTables">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Date_time)
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Date_time)
                    </td>
                </tr>
            }
        </tbody>
    </table>

    @section Scripts {
        <link rel="stylesheet" type="text/css" href="~/lib/DataTables/datatables.css">
        <script type="text/javascript" charset="utf8" src="~/lib/DataTables/datatables.js"></script>
        <script type="text/javascript" src="~/lib/DataTables/moment.js"></script>
        <script type="text/javascript" src="~/lib/DataTables/datetime-moment.js"></script>
        <script>
            $(document).ready(function () {
                $.fn.dataTable.moment('HH:mm MMM D, YY');
                $('#tableDataTables').DataTable();
            });
        </script>
    }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Happy to take a look at a test case showing the issue.

    Allan

  • FrefiFrefi Posts: 2Questions: 1Answers: 1
    edited April 2019 Answer ✓

    Hello, again! :)
    Thank you Allan for taking your time to looking into this!

    I actually found the "answer" to my question. I was wondering what the datetime looked in the database and i was different (2018-05-03 15:23:20.000) from what I wrote in my initial question.
    So I tried to change the displayformat, of the datetime variable in my Model-code, like this: [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] & it seems to be working.
    I can sort the datetime column but I can also remove Moment.js and code, so Im not quite sure what I have accompliced :)

    Read about displayformat here. Guess it was a noob mistake :)

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Perfect - thanks for posting back with your solution!

    Allan

This discussion has been closed.