Date Format and Sorting Issue

Date Format and Sorting Issue

ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0
edited September 2023 in Free community support

I am passing a date in the format "2023-07-30 00:00:00.000" from a JSON source,
but when displayed in the datatable, it appears as "/Date(1690660800000)/".

I am using using moment.min.js ,datetime-moment.js and " $.fn.dataTable.moment('DD/MMM/YYYY');"
to format the date as "30 Jun 2023".

I am looking for guidance on how to resolve this issue and achieve the desired date format in the datatable.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421

    Try this for your column:

    columns: [
        {   data: "yourDateField",
            render: function ( data, type, row ) {
                return moment(data).format('DD/MMM/YYYY');
            }
        },
    

    The following isn't really used for rendering but to tell the ultimate date sorting plugin what the date format is:

    $.fn.dataTable.moment('DD/MMM/YYYY');
    

    https://datatables.net/blog/2014-12-18

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0

    I successfully corrected the date format display issue,
    but now I am facing a problem with sorting. The sorting treats the dates as alphanumeric values rather than actual dates. You're seeking a solution to ensure that the sorting is done in proper date format.


    above image is on page load

    After sorting ascending and descending.

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    Can you link to an example showing thebissue ao we can helpto debug it please?

    Allan

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421
    Answer ✓

    Maybe one additional hint. This line of code must be executed BEFORE data table initialization.

    $.fn.dataTable.moment('DD/MMM/YYYY');
    
  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0

    @rf1234 Thanks

    its worked

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0
    edited September 2023

    Thanks all for instant Support

Sign In or Register to comment.