JSON Linq.js filter on start date end date column

2.3k views Asked by At

I need to filter results between start date and end date selected by datepicker using reference http://linqjs.codeplex.com/ how can I filter based on my calendar inputs.

start date : $('#startdate').val() // MM/DD/YYYY format

end date : $('#enddate').val() // MM/DD/YYYY format

var queryResult = $.Enumerable.From(jsonResultTble) 
.Where("??") 
.ToArray();

Sample Data:

var jsonResultTble = [{"ItemId":3,"Condition":"Very Good","Seller":"[email protected]","Rating":0,"School":"University of California-Los Angeles","City":" ","State":" ","Comments":"N/A","RetailPrice":0,"ManufactureDate":"4/10/2012 12:00:00 AM","ExpiryDate":"4/10/2014 12:00:00 AM"},{"ItemId":4,"Condition":"Very Good","Seller":"[email protected]","Rating":18,"School":"Mississippi Valley State University","City":" ","State":" ","Comments":"N/A","RetailPrice":0,"ManufactureDate":"1/10/2010 12:00:00 AM","ExpiryDate":"4/10/2016 12:00:00 AM"]; 
1

There are 1 answers

0
Faris Zacina On BEST ANSWER

You can do the following to filter based on a date range:

var startDate = "1/10/2010";
var endDate = "4/10/2010";

var queryResult = Enumerable.From(jsonResultTble)
    .Where(function (x) { return x.ManufactureDate >= startDate && x.ManufactureDate <= endDate })
    .OrderBy(function (x) { return x.Seller })
    .Select(function (x) { return x.Seller })
    .ToArray();

Here is a jsFiddle for the LINQ.js:

http://jsfiddle.net/6mchrmn9/5/

If you want to get rid of the time in the json, instead of x.ManufactureDate you can re-format the code using something like:

new Date(x.ManufactureDate).format("dd/m/yyyy");

The format function does not exist natively on the prototype, so check out:

http://jsfiddle.net/phZr7/508/