rowCallback with Date()

rowCallback with Date()

antoniocibantoniocib Posts: 277Questions: 62Answers: 1

Hello everyone I'm trying to use rowCallback for the rows that if they have a date lower than today's must be highlighted.
It just doesn't work why?
I enclose code:

rowCallback: function ( row, data ) {                                                                                                                   
if ( data.data_scadenza < today) {
$(row).addClass('myHighlight1')
$(row).removeClass('myHighlight')
}
},
var today = new Date();

This is the console.log of var today

My format date is d/m/Y

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    If you debug the rowCallback you will probably find that data.data_scadenza is a string not a date. You may need to do something like this:

    if ( new Date( data.data_scadenza ) < today) {
    

    If you still need help then please provide a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren in your way not works, with this code you see what output it gives me

    today = new Date();
    var dateString = today.format("dd/mm/yyyy");
    console.log(dateString);
    rowCallback: function ( row, data ) {                                                                                                                   
    console.log(data.data_scadenza);
    if (  data.data_scadenza < dateString  ) {
        $(row).addClass('myHighlight1')
        $(row).removeClass('myHighlight')
        }
    }
    

    OUTPUT:

    The date of today is 23/05/2022 and the IF CLAUSE not works correctly

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    The first step is to determine the data type of data.data_scadenza. You are creating a date object with today = new Date();. data.data_scadenza needs to also be a date object for the comparison to work. Let us know what you find.

    In order to help we will need to see an example of the data you are working with. If you still need help then please post a test case replicating the issue so we can offer more specific suggestions.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Debugging the rowCallback indicates data.data_scadenza is a string:

    You will need to convert it to a date object. Similar to my code snippet above.

    if ( new Date( data.data_scadenza ) < today) {
    

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @kthorngren
    Hi Kevin, as you can see I changed the clause as you said only that Date uses a different date format so the check gets it wrong

    Looks on http://comidb.it/scadenze/scrivania.php

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770
    edited May 2022

    Looks like its working to me:

    Please explain what is wrong.

    My suggestions is that you use the browser's debugger to step through the rowCallback to see what its doing. Or at a minimum use a console log statement to see what the values are, something like this:

    console.log( new Date( data.data_scadenza ), today, new Date( data.data_scadenza ) < today );
    

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    edited May 2022

    @kthorngren today is 24/05/2022.

    My format date is DD/MM/YYYY, so the 06/01/2022 need to be red not white

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Take a look at using moment.js to format the dates when comparing.

    Kevin

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited May 2022

    So you need to create the "data_scadenza" date object using the proper input format. You can be sure that 6 January 2022 provided as 06/01/2022 will be disambiguated by Javascript using the American date format. Hence "data_scadenza" will be interpreted to be 1 June 2022.

    Just provide the input date in format YYYY/MM/DD. Then it should work

    var d = data.data_scadenza;
    newDate(d.substr(6,4) + d.substr(2,4) + d.substr(0,2))
    

    As @kthorngren recommends it doesn't hurt to use moment.js. In case you have a lot of date manipulations it is worth the effort to get started with moment.js.

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    sorry typo: forgot the blank between "new" and "Date"

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    edited May 2022
    Editor::inst( $db, 'scadenze', 'id' )
    ->fields(
    
    Field::inst( 'data_scadenza' ),
    //  ->validator( Validate::dateFormat( 'd/m/Y' ) )
    //  ->getFormatter( Format::dateSqlToFormat( 'd/m/y' ) )
    //  ->setFormatter( Format::dateFormatToSql( 'd/m/Y' ) ),
    
    var today = new Date();
    rowCallback: function ( row, data, ) 
    {                                                                                                                   
    $('input.check_p', row).prop( 'checked', data.check_p == 1 );
    console.log( new Date( data.data_scadenza ), today, new Date( data.data_scadenza ) < today );
    if ( new Date( data.data_scadenza ) <  today)
    {
    count++;
    $(row).addClass('myHighlight1')
    $(row).removeClass('myHighlight')
    }
    },
    

    CONSOLE

    OUTPUT

    Now my format date is yyyy-mm-dd and works if i want format date dd/mm/yyyy how i do?
    @rf1234 @kthorngren

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Instead of making the changes in your server script did you try the option rf1234 presented?

    var d = data.data_scadenza;
    new Date(d.substr(6,4) + d.substr(2,4) + d.substr(0,2))
    

    Seems like that should provide the needed date format for your if statement.

    Kevin

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    I tried it with your data and it seems to work:

    Kevin

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited May 2022

    Thanks Kevin! All Europeans share the same problem when it comes to date formatting in "US centric" programming languages like Javascript or PHP. Our date formats are chiefly DD.MM.YYYY, DD/MM/YYYY, DD/MM/YY, DD MM YYYY etc.

    But we never have a month-day-year sequence. At least none I am aware of.

    About as foreign as having pounds, gallons and inches :smiley:. Sorry, but I had to do this one, lol. I hope I do not offend any American or English member of the forum with this! When I went to school in Pennsylvania I learned that I am not 190cm tall but 6 foot 4. Confusing. :smile:

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Our date formats are chiefly DD.MM.YYYY, DD/MM/YYYY, DD/MM/YY, DD MM YYYY etc.
    But we never have a month-day-year sequence. At least none I am aware of.

    Thats because we Americans use inches so we need a simple way to find the month first - LOL

    Kevin

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    I agree, Kevin :smiley:

    @antoniocib
    I tried this

    new Date("13/06/2022");
    

    and it returns "invalid date". No month 13 in America.
    While this

    new Date("2022/06/13");
    

    returns the proper date. 13 June 2022.

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1
    edited May 2022

    @rf1234 and @kthorngren

    rowCallback: function ( row, data, ) {                                                                                                                  
    $('input.check_p', row).prop( 'checked', data.check_p == 1 );
    console.log( new Date( data.data_scadenza ), today, new Date( data.data_scadenza ) < today );
    
        var d = data.data_scadenza;
    new Date(d.substr(6,4) + d.substr(2,4) + d.substr(0,2))
        if ( d <  today){
        count++;
        $(row).addClass('myHighlight1')
        $(row).removeClass('myHighlight')
    }
    },
    
    Editor::inst( $db, 'scadenze', 'id' )
        ->fields(
    
    Field::inst( 'data_scadenza' ),
    ->validator( Validate::dateFormat( 'd/m/Y' ) )
    ->getFormatter( Format::dateSqlToFormat( 'd/m/Y' ) )
    ->setFormatter( Format::dateFormatToSql( 'd/m/Y' ) ),
    
    

    in this way not works..

    looks on http://www.comidb.it/scadenze/scrivania.php

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    Answer ✓

    No it can't work this way, Antonio because "d" is not an object but merely your date string. if "today" is today's date object you must compare it with the date object derived from data.data_scadenza not with data.data_scadenza itself.

    var d = data.data_scadenza;
    
    if ( new Date(d.substr(6,4) + d.substr(2,4) + d.substr(0,2)) < today ) {
       count++;
        $(row).addClass('myHighlight1')
        $(row).removeClass('myHighlight')
    }...
    
  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    @rf1234 @kthorngren AAAAAAA NOW WORKS I LOVE IT! Thank you guys

Sign In or Register to comment.