# Filter KV by date?

I’m in real need of filtering keys by date, but I’m unable to figure out a clever way to use the prefix to filter between start and end range.

Anyone have any ideas on how to hack this?

EDIT: Figured it out!

Since prefix is text-based it will filter everything that it starts with, but we need some number logic to make sense of it in the KV.

Say we have a key with a March date:
`2019-3456789101112`

Then we just use the prefix filter:
`2019-3456789101112`

…to get everything that’s from march and forward.

We also want the end date, so…

Say we have a key value of: `[email protected]`

Entering the prefix:
`[email protected]`

Will give us all keys from March to July.

The same logic applies to days.

1 Like

Just a tough, year Decimal, month in hexadecimal, days decimal.

1January 2001
2001101

24 October 2019
2019A24

1 Like

Does that work? Prefix don’t care about sizes of numbers, only what strings begin with.

And I feel it adds a ton of complexity and makes it really hard to reason about.

Of course, doing it the way I suggested create really long keys, but I see no other way.

1 Like

sorry my ignorance, but how are you getting a range? I don’t see the logic here

So you use these for both the label and prefix.

To list all dates (2019): `[email protected]`

To list (2019) April > August: `[email protected]`

This is range-based, so a KV key will have a start-date and an end-date, if you only want the same month or day, you just set from that date to that date.

Example (2019-03-30): `2019-34[email protected]2019-1211109876543-123456789101112131415161718192021222324252627282930`

Like I wrote in the first entry, these keys will be long, but it’s still the only way to handle dates.

Also, keep in mind that to do this correctly, you’ll need to use the JS date function to get the correct amount of days per month. And of course, you’ll need to be consistent, if you are EVER going to use days or time-stamps, you’ll need to include it from the get-go.

Does the @ have a function? Is there documentation on this? Or how does the range work? Because of the moment of wrting the key, we only know 1 date…

It’s all just text mate and @ is just a separator, you can use anything.

No docs, this is all just a “hack” to get this working.

If you only know 1 date, then set both start and end, to the same date.

Simple example:

1. Create a KV and name it: “[email protected]”.
2. Create another one, name it: “[email protected]
3. Use the KV list function with prefix: “[email protected]” and you will get both keys in the list.
4. Instead use the prefix “[email protected]” and you will only get the 1 key.

This method works the same way, just ignore the numbers and treat it as strings.

So when you do the writes you already know the start and end date of the key?