0

In my html I have two fields, the start date and the end date and then a table below that I would like to soft through.

I have it to the point where I can show and hide if the date is equal to what is in the input, but I seem to be having trouble seeing if it is in between the two dates.

So to start with I'm grabbing the start and end dates

var start = $(this).parent().find('.startdate').find('input').val();
var end = $(this).parent().find('.enddate').find('input').val();

and then I'm looping through all the dates in the table and grabbing their value

$($('.revRec')).each(function( index ) {
  var dateText = $(this).text();
});

And then within that loop I am trying to see if the date in the table falls in-between the start and end dates.

I was doing something like this, which I know is not right

if( $(this).text() == start){do stuff}

Should I be casting these values to numbers or dates? I've never worked with dates before or comparing them. Any help would be appreciated!

zazvorniki
  • 3,512
  • 21
  • 74
  • 122

1 Answers1

2

You can do comparison as strings if you want--provided the date format is consistent. In that case your comparison would look like

if( $(this).text() >= start && $(this).text() <= end) {do stuff}

But why do that when JQuery already has? There's a pretty simple date-range plugin here that you can take a look at. The plugin adds the filter as a table header so you may want that or may not, it's up to you.

Nathan Foss
  • 595
  • 1
  • 9
  • 34
  • This worked perfectly, thank you. The reason why I'm not using a plugin here is because the application I'm working with already has an overabundance of them and I actually trying to remove some. For example that one line of code is removing three different plugins. – zazvorniki Apr 02 '14 at 12:50
  • Actually just found an issue with this answer. It works if you are within the same month...for example if you search from between the first of march and the end of march...but if you search between February and March dates from April still appear. – zazvorniki Apr 02 '14 at 13:37
  • That's probably because your date format is yyyy-dd-mm. So the string comparison will return its result from the day field more often than not. To fix this, either change your date format to yyyy-mm-dd or convert the string to a Date object. – Nathan Foss Apr 02 '14 at 14:02
  • How would I go about converting it to a date object? Just wrapping them in Date()? When I do the date comes out wrong. It changes April to January – zazvorniki Apr 02 '14 at 14:09
  • http://stackoverflow.com/questions/8224459/how-to-create-a-date-object-from-string-in-javascript – Nathan Foss Apr 02 '14 at 14:17
  • Then this for comparing dates http://stackoverflow.com/questions/492994/compare-dates-with-javascript – Nathan Foss Apr 02 '14 at 14:19
  • Yes, I have read that (both of them). My issue is that because my date is backwards it is reading it wrong. Is there a way to format the date object so it reads it the right way or do I have to split each field up and do it the long way? – zazvorniki Apr 02 '14 at 14:20
  • From what I can tell, you'll have to write your own function and call it on each date (whether object or string) – Nathan Foss Apr 02 '14 at 14:30