check if date is older then today

check if date is older then today

alviasalvias Posts: 11Questions: 2Answers: 1

I have another Question, so i opend a new one

I have to change the color of an enty if the date is older then the current date

i would like to do it with those lines but need to know how to get the current date

/**
* name column 2
*/
"columnDefs": [
{ className: "payed_untill", "targets": [ 1 ] }
],

/**
* color not payed red
*/

"fnRowCallback":function(nRow, aData ) {
if (aData[1] < -->current date somehow<-- ) {
jQuery('td.payed_untill', nRow).css('color', 'red');
}
},

Thank you in advance

This question has an accepted answers - jump to answer

Answers

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14

    Are you using Moment.js?

  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    Agreed - Moment is what I always reach for when doing date / time calculations in Javascript. It can be overkill sometimes, for example if your aData[1] was ISO8601, then it would be easy to use Date.parse() to get it and new Date() to get the current day and subtract one for the comparison.

    Allan

  • alviasalvias Posts: 11Questions: 2Answers: 1

    did not know about it, thank you, it works now

  • alviasalvias Posts: 11Questions: 2Answers: 1

    hmm or maybe not.
    it colors only one field
    honestly, i suck at this :tired_face:

                                    "fnRowCallback":function(nRow, aData ) {
                                        if (aData[1] < moment().format('L') ) {
                                        jQuery('td.payed_untill', nRow).css('color', 'red');
    
                                    }
                                    },
    
  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    What is aData[1]? i.e. what is the data in it? If it is a string, you'll need to convert it to be a date, as I mentioned above.

    Allan

  • alviasalvias Posts: 11Questions: 2Answers: 1

    Sorry for the late answer.
    aData[1] is the "payed untill" filed you can see in the picture below.

    So what i need is, that the date turns red if the actual date is past the date in the filed.

  • alviasalvias Posts: 11Questions: 2Answers: 1
    edited May 2017 Answer ✓
                                "columnDefs": [
                                    { className: "payed", "targets": [ 1 ] }
                                        ],
    
                            /**
                            * color not payed red 
                            */
    
                                    "fnRowCallback":function(nRow, aData ) {
                                        if (aData[1] < moment().format('DD/MM/YYYY')  ) {
                                        jQuery('td.payed', nRow).css('color', 'red');
    
                                    }   else { jQuery('td.payed', nRow).css('color', 'green');
    
                                    }
                                    },
    

    Ok, so this is actually working.

    The problem was really dumb.
    I am writing on a german PC in Spain so the spanish date is written DD/MM/YYYY and german its DD.MM.YYYY.

    Just had to sort that out and now it works almost fine.

    its seems to work better by setting .format to:
    .format('YYYY/MM/DD')

  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    Thanks for posting back. Good to hear you have it working now.

    Allan

This discussion has been closed.