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!

2
  • Create a jsFiddle please. Commented Apr 1, 2014 at 20:59
  • @j08691 A fiddle jsfiddle.net/g63yh/1 Commented Apr 1, 2014 at 21:08

1 Answer 1

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.

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

8 Comments

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.
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.
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.
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
|

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.