i want to sort datatable by date but its treated as string i want to set as date

i want to sort datatable by date but its treated as string i want to set as date

Propix_311Propix_311 Posts: 5Questions: 2Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • Propix_311Propix_311 Posts: 5Questions: 2Answers: 0

    "_aSortData": [null, null, { "sType": 'date-uk' }, null],

    i want to sort 2nd column .sorting are done but asc/desc not working for particular column

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited October 2020

    This blog discusses the recommended way to sort date columns. If this doesn't help then as Colin asked please provide a test case showing you dates so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Propix_311Propix_311 Posts: 5Questions: 2Answers: 0

    How to date sort as a date instead of string DataTables

    datetable.js file is common and my date column is not fixed in another table.

    if I am passing date to column then column sort on date type

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    The blog I linked to provides the solution to sorting dates. Setting the columns.type won't solve the problem. Datatables will automatically determine the column type for certain date formats. If your date format is not automatically detected then you need to use the solution in the blog to provide Datatales with the date format you are using.

    Kevin

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Please re-read the replies you have already been given.

  • Propix_311Propix_311 Posts: 5Questions: 2Answers: 0

    my date column contains date and time please suggest where can I set column type =date in my code

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited October 2020

    Did you read the blog article I linked to? It tells you what to do to sort datetime columns if they are treated as strings.

    This is the third time I've provided the solution. If its not working for you or you have questions about out let us know. Simply asking the same question over and over is not going to give you a different answer.

    Or provide a running test case, with examples of your datetime values, that way we can show you how it works with your specific datetime format.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • stevecookmsstevecookms Posts: 3Questions: 1Answers: 0

    are you filling the datatable via ajax or pre loading the data?

    if via ajax,then just change the column sortby on the back end server.

    if via preload data, then use something like this..

    this is .net MVC

    var actionDate = TimeZoneInfo.ConvertTimeFromUtc(item.ActionDate, TimeZoneInfo.FindSystemTimeZoneById(ViewBag.UserTimeZoneId)).ToString("yyyy-MM-dd-HH:mm:ss");
    
    var actionDateDisplay = TimeZoneInfo.ConvertTimeFromUtc(item.ActionDate, TimeZoneInfo.FindSystemTimeZoneById(ViewBag.UserTimeZoneId)).ToString("yyyy-MM-dd hh:mm:ss tt");
    
    
    <td data-sort="@actionDate">
        @Html.DisplayFor(modelItem => actionDateDisplay)
    </td>
    
This discussion has been closed.