2

In my web page, I have a table of dates (my date format is dd-mm-yyyy)

<tr>
    <td>05-03-2012</td>
    <td name="expiration">09-07-2012</td>
    <td>08-10-2012</td>
 <tr>

What I want is to put in red color expiration dates when their value is > current date, using Jquery.

So, I think I am going to use something like :

<script>
//don't know how to retrieve the expiration date and check if it is after the
//current date 

$('td[name=expiration]').css({"color":"red"});
</script>
2
  • I can help you with this, but I need to know if your date formate will always be mm-dd-yyyy Commented Jul 9, 2012 at 1:24
  • Ok thanks! My date format will always be dd-mm-yyyy actually as I am using Twig using {{ object.dateExpiration | date (d-m-Y) }} to display this date. Commented Jul 9, 2012 at 12:52

3 Answers 3

3

Prefer to use a class instead of a name so your selector is more efficient:

<tr>
    <td>05-03-2012</td>
    <td class="expiration-date">09-07-2012</td>
    <td>08-10-2012</td>
</tr>

var now = new Date;
$('.expiration-date').text(function (i, v) {
    if (now < new Date(v)) {
        $(this).addClass('unexpired');
    }
});

Here's an example in a jsFiddle: http://jsfiddle.net/qRdNe/

Sign up to request clarification or add additional context in comments.

4 Comments

console.log( new Date(v) ); returns invalid date, does this code really work
It works just fine, as demonstrated by this modified fiddle: jsfiddle.net/qRdNe/6
There is a problem with the date format, when I add var date = new Date(v); alert(date); I have an alert "Invalid date". (My format is dd-mm-yyy).
OK so I had to parse the date using var str = v.split("-"); var date = new Date(str[2],str[1]-1,str[0],0,0,0,0); I don't know why but if I kept str[1] it will add a month . Thanks your help!
2

This doesn't account for any errors in date time parsing.

$('td[name=expiration]').each(function(){

    var innerText = $(this).text();
    var date = new Date(innerText);
    var today = new Date();    

    if(date > today){
       //Set css here
    }
    else{
       //Set default here
    }

});

3 Comments

to handle date parsing, use this answer, but add the following lines (above the first line of the function, and change the Date object construction for the variable date as described below: var str = innerText.split("-"); var date = new Date(str[2],str[0],str[1],0,0,0,0);
There is no need to go to such lengths to parse the Date string. The Date object will do it just fine for you: jsfiddle.net/qRdNe/6
there are some strings the Date object can handle if passed directly, this is not one of them. If you try to initiate the Date object with the text of once of these cells, you'll get the wrong date. Using the string from her example, you get Dec 31, 1969.
0

I have basically put together a working answer its from different bits given here but corrected where needed, the date for a month starts from zero. Maybe you don't want to accept this as the right answer , i'm just putting all of it here so its easy to reference.

$('.expiration').text(function(i,v){
    var datestr = v.split("-");
    d = new Date(datestr[2],datestr[0]-1,datestr[1],0,0,0,0);
    var now = new Date();        
    if(console)console.log(d);        
    if(d>now )
             {
                $(this).css("color","red");            
             }                

});

http://jsfiddle.net/6zb5W/3/

3 Comments

This fiddle is the same version number as mine
In FF 12 im using it gives an invalid date, even if the date is before now it still highlights the date
Looks to be a FF specific issue. The date parses fine in Chrome and IE9, but FF 13 does not handle the date correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.