# Data filter for date range

**URL:** <https://talk.observablehq.com/t/data-filter-for-date-range/6031>\
**Category:** Help\
**Created:** [January 4, 2022, 12:02am UTC](https://talk.observablehq.com/t/data-filter-for-date-range/6031 "2022-01-04T00:02:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dennis](https://avatars.discourse-cdn.com/v4/letter/d/e56c9b/32.png) [@Dennis](https://talk.observablehq.com/u/Dennis)\
**Post date:** [January 4, 2022, 12:02am UTC](https://talk.observablehq.com/t/data-filter-for-date-range/6031/1 "2022-01-04T00:02:24Z")

</div>

My data (json) has a date field that looks like this:

“occurrencedate”: “2022-01-03 13:05:33 -0500”,

I’m trying to filter it to a range…

viewof start = Inputs.date({label: “Start date”, value: “2021-10-01”})  
viewof end = Inputs.date({label: “End date”, value: “2021-12-31”})

data.filter(d =\> d.occurrencedate \>= start && d.occurrencedate \<= end)

Nothing ever comes back. I suspect it could be that the data value is not recognized as a Date type (I’m not a JavaScript programmer). I’ve tried adding Data.Parse() which does get some results, but it’s not correct.

data.filter(d =\> Date.parse(d.occurrencedate) \>= Date.parse(start) && Date.parse(d.occurrencedate) \<= Date.parse(end))

What’s the correct syntax for the filter?

---

<div class="post-metadata">

**Author:** ![tophtucker](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tophtucker/32/4781_2.png) [@tophtucker](https://talk.observablehq.com/u/tophtucker)\
**Post date:** [January 4, 2022, 12:55am UTC](https://talk.observablehq.com/t/data-filter-for-date-range/6031/2 "2022-01-04T00:55:54Z")

</div>

It seems like you must almost have it!

You’re right that you have to parse the dates when they come from JSON (from which they’ll come as strings). Note that Date.parse returns a number, not a date, but it should be OK because the \>= comparison in the filter will turn dates to numbers anyway. Date.parse is used behind-the-scenes when you do `new Date("2022-01-03 13:05:33 -0500")`, which should usually work, although it has some cross-browser compatibility issues, so you can use [d3-time-format](https://github.com/d3/d3-time-format) for a stricter parser.

Your filter syntax looks good, though you shouldn’t have to parse `start` and `end` because Inputs.date already returns a date.

Can you share a link to your notebook, or to a sample notebook that demonstrates the issue? Here’s a working example in case that helps:

> **[Filter dates from JSON](https://observablehq.com/d/f156b91834ed0f22)**
>
> Dennis asked in the forum about filtering JSON data by date. You had the right idea, and I think you must be almost there, but maybe it’ll help to see a working example. When you serialize dates to JSON and parse them back to JavaScript, they become...
