2

I am getting two values from DatePicker (End,Start). I'd like to filter data and get data between those two values.

But i couldn't get it to work the write way.

To focus on the problem, Let's consider the both value (Start Date and End Date)

// Here I want to 
//var myDate = new Date();
var startDate = new Date(2013, 1, 28);
var endDate = new Date(2013, 1, 30);

The data I have is like :

var filter_data = {
    "type": "FeatureCollection",
    "features": [{
        "geometry": {
            "type": "Point",
            "coordinates": [-73.98115508098644, 40.755304404503995]
        },
        "type": "Feature",
        "id": 0,
        "properties": {
            "Date": "01/20/2013 08:58:03 PM",
            "lat": 40.755304404503995,
            "lon": -73.98115508098644,
            "Address": "33 WEST 44 STREET"
        }
    },, {
        "geometry": {
            "type": "Point",
            "coordinates": [-73.968253883967947, 40.765050901514641]
        },
        "type": "Feature",
        "id": 1,
        "properties": {
            "Date": "01/29/2013 02:53:11 PM",
            "lat": 40.765050901514641,
            "lon": -73.968253883967947,
            "Address": "564 PARK AVENUE"
        }
    }, 
                ]
};

To filter the data, I used two different approaches, but none of these approaches work.

var filteredData_2 = filter_data.where( ( n, i ) => n.properties.Date <startDate && n.properties.endDate ) ;

 var filteredData = filter_data.filter(function(a){return a.properties.Date >startDate && a.properties.Date <endDate;});


console.log(filteredData_2)

console.log(filteredData)

Here is the like to JsFiddle:

http://jsfiddle.net/dnvrw/1/

2 Answers 2

1

First of all, you need to convert your string into a JavaScript Date() object:

function parseDate(dateString){
     var dateParts = dateString.split(' ');
     var dateArr = dateParts[0].split('/');
     var timeArr = dateParts[1].split(':');
     if(dateParts[2] === 'PM'){
         timeArr[0] += 12;   
     }
     return new Date(dateArr[0], dateArr[1], dateArr[2], timeArr[0], timeArr[1], timeArr[2]);
}

Then you need to actually filter on the features array:

 var filteredData = filter_data.features.filter(function(a){return parseDate(a.properties.Date) > startDate && parseDate(a.properties.Date) < endDate;});

EDIT: moonwave99 shows that you can safely create a Date() by passing the string as an argument, reducing the solution to:

 var filteredData = filter_data.features.filter(function(a){return new Date(a.properties.Date) > startDate && new Date(a.properties.Date) < endDate;});

Updated fiddle:

http://jsfiddle.net/emVJV/2/

Fiddle for simplified answer:

http://jsfiddle.net/2rXBN/2/

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

1 Comment

It should be "> startDate" and not " < startDate". Correct me if I am wrong
1

You are comparing Date objects [startDate, endDate] with plain strings.

Wrap a.properties.Date into a proper Date object:

... function(a){
  aDate = new Date(a.properties.Date);
  aDate < startDate && aDate < endDate;
})

Comments

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.