Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jan 2005
    Location
    Memphis, TN
    Posts
    1,785
    Thanks
    8
    Thanked 131 Times in 129 Posts

    Comparing dates in a table

    My turn to ask

    I have a table basically like this:

    Code:
    <tr>
    <td>09.11.2010</td><td>11:30 AM</td><td>Location</td><td>Team</td><td>Outcome</td>
    </tr>
    <tr>
    <td>10.29.2010</td><td>11:30 AM</td><td>Location</td><td>Team</td><td>Outcome</td>
    </tr>
    <tr>
    <td>11.7.2010</td><td>11:30 AM</td><td>Location</td><td>Team</td><td>Outcome</td>
    </tr>
    What I am trying to do is grab the first cell (the date) and that's easy enough. What I am struggling with is comparing that date to today's date. If it's in the past ignore it. If it's in the future and the first one in the future, do my function.

    So in the sample table above, it would ignore the first line, stop at the second as it's the next date from today and run my function.

    I just need a hand converting today into a string that I can compare to the data in the table correctly.

    Essentially I think the start is somethign liek:

    Code:
    function nextGame() {
    	$('#foo').load('schedule.html .scheduletable', function() {														   	
    		$('#foo tbody tr').each(function(){	
    			var startdate= $(this).children('td:eq(0)').text();
    				// beats me
    		});														
    	});
    }
    Last edited by harbingerOTV; 10-25-2010 at 07:50 PM.
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,955
    Thanks
    9
    Thanked 726 Times in 720 Posts
    I just need a hand converting today into a string that I can compare to the data in the table correctly.
    This will convert today's date to dd.mm.yyyy:

    Code:
    var now = new Date();
    var $datestring = now.getDate()+"."+(1+parseInt(now.getMonth()))+"."+now.getFullYear();
    EDIT: You'll want mm.dd.yyyy of course, being transatlantic....

  • #3
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,955
    Thanks
    9
    Thanked 726 Times in 720 Posts
    I missed the leading zero on that...

    ..but, it would be better I think to convert your string to a date before comparison - this would make the past/future thing easier.

    So:

    Code:
    var startdate= $(this).children('td:eq(0)').text();
    startdate = startdate.replace(/[.]/g,"/");//replace dots with slashes
    var checkdate = new Date(startdate);//convert to a date
    var now = new Date();
    if (checkdate < now).....do whatever

  • Users who have thanked SB65 for this post:

    harbingerOTV (10-26-2010)

  • #4
    Senior Coder
    Join Date
    Jan 2005
    Location
    Memphis, TN
    Posts
    1,785
    Thanks
    8
    Thanked 131 Times in 129 Posts
    @SB65,

    Awesome, thanks so much. Some reason I've never wrapped my head around JS dates. With your help my script ballooned up to what I wanted it to be:

    Code:
    			$('#foo .scheduletable tbody tr').each(function(){	
    				startdate= $(this).children('td:eq(0)').text();
    				startdate = startdate.replace(/[.]/g,"/");//replace dots with slashes
    				checkdate = new Date(startdate);//convert to a date
    				now = new Date();
    				now.setHours(0,0,0,0);
    					//alert(checkdate);
    					//alert(now);
    				if (checkdate < now){
    					$(this).remove();
    				}
    			});	
    						gamedate = $('#foo .scheduletable tbody tr:eq(0)').children('td:eq(0)').text();
    						gamedate = gamedate.replace(/[.]/g,"/");
    						gamedate = new Date(gamedate);
    						now = new Date();
    						now.setHours(0,0,0,0);
    						if(gamedate <= now){
    							mensGameLeader = 'Today\'s Men\'s Game:';
    						} else {
    							mensGameLeader = 'Next Men\'s Game:';
    						}
    						//alert('gamedate= '+gamedate2+' :: now= '+now);
    						mensDate = $('#foo .scheduletable tbody tr:eq(0)').children('td:eq(0)').text();
    						mensTime = $('#foo .scheduletable tbody tr:eq(0)').children('td:eq(1)').text();
    						mensLocation = $('#foo .scheduletable tbody tr:eq(0)').children('td:eq(2)').text();
    						mensOpponent = $('#foo .scheduletable tbody tr:eq(0)').children('td:eq(3)').text();
    						
    						if(mensDate != ''){
    						$('#nextGame').append('<div class="mensGame"><b>' + mensGameLeader + '</b> ' + mensDate + ' - ' + mensTime + ' @' + mensLocation + ' vs. ' + mensOpponent + '</div>');
    						$('#nextGame').animate({'width' : '940px'}, 600).delay(2000).queue(function(){
    							womensGame();
    						});
    						}
    Then a similar function for the Woman's team.

    Your guidance helped me doing:
    Code:
    now.setHours(0,0,0,0);
    						if(gamedate <= now){
    							mensGameLeader = 'Today\'s Men\'s Game:';
    						} else {
    							mensGameLeader = 'Next Men\'s Game:';
    						}
    as well to indicate that a game was on the current day.

    Pretty sweet, thanks again!
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •