Date range filter with dd-mm-yyyy format

Date range filter with dd-mm-yyyy format

Blackbull256Blackbull256 Posts: 4Questions: 2Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem: I want to add a date filter to my datatable. I used the example posted here, but I can't get it to work with the dd-mm-yyyy date notation. Does anyone have a working example of a date range filter with this notation?

This question has an accepted answers - jump to answer

Answers

  • sinfuljoshsinfuljosh Posts: 28Questions: 0Answers: 5
    edited February 2022

    http://live.datatables.net/polaloto/2/edit
    Modified the range filter from the demo to use the DD-MM-YYYY format.

    Also replaced 6-7 with new lines 6-12 to fix a bug where it was not zero'ing out the time on date filters.

    If you include Moment.js

    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.11.4/date-1.1.1/datatables.min.js"></script>
    

    in your js scripts you can replace 6-12 with this for better filtering (the below code will return no records on a badly formatted date on the min/max inputs)

    let min = moment($('#min').val()).isValid() ?
        new Date( $('#min').val() ).setUTCHours(0,0,0,0) :
        null;
      let max = moment($('#max').val()).isValid() ?
         new Date( $('#max').val() ).setUTCHours(23,59,59,999):
         null;
    
  • Blackbull256Blackbull256 Posts: 4Questions: 2Answers: 0

    Hey Josh,

    Thanks for your answer. I have created a test case with the date format in my application, and your solution does not quite seem to work. Do you have any idea why?

    http://live.datatables.net/jewumadi/1/edit

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    There were a few problems, mostly due to the mixing of the Date and Moment formats, which meant the checks weren't using "DD-MM-YYYY". I tidied it up and this appears to be doing what you're after: http://live.datatables.net/jewumadi/2/edit

    Colin

  • Blackbull256Blackbull256 Posts: 4Questions: 2Answers: 0

    Thanks Colin, this works perfectly!

Sign In or Register to comment.