# Time filter for object class data source

**URL:** <https://community.appfarm.io/t/time-filter-for-object-class-data-source/523>\
**Category:** Ask the community\
**Tags:** filtered-selection\
**Created:** [June 8, 2023, 1:45pm UTC](https://community.appfarm.io/t/time-filter-for-object-class-data-source/523 "2023-06-08T13:45:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OleSorheim](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/olesorheim/32/1171_2.png) [@OleSorheim](https://community.appfarm.io/u/OleSorheim)\
**Post date:** [June 8, 2023, 1:45pm UTC](https://community.appfarm.io/t/time-filter-for-object-class-data-source/523/1 "2023-06-08T13:45:18Z")

</div>

Hi! I’m trying to filter my data based on input. I want to be able to quickly select last week etc. **OR** select a specified timerange. The default should be “last week”.  
This is my current setup:  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/appfarm/original/1X/6305ba228a57c9e55588a829ff4a0178cae52d09.png)

This function should translate “Last week” to a datetime, but does not work. I think the function is stolen from an Appfarm showroom app:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/appfarm/original/1X/b8335b38c6db296953e04794afda5ad64dbd6d41.png)

Have tried this but it does not satisfy my needs:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/appfarm/original/1X/fe438ed7d2333c4fb0820bf4dd162218755f741c.png)

So what is the best way to filter a datasource based on a time-range?  
And how can I set a condition where you can **either** select a specific time-frame or “last week”, “last month” etc.?

---

<div class="post-metadata">

**Author:** ![Eirik](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/eirik/32/76_2.png) [@Eirik](https://community.appfarm.io/u/Eirik)\
**Post date:** [June 9, 2023, 7:02am UTC](https://community.appfarm.io/t/time-filter-for-object-class-data-source/523/2 "2023-06-09T07:02:55Z")

</div>

Hi (and welcome to the Community!)

A few pointers for a possible solution:

- You would need three app variables, `Time From` (datetime), `Time To` (datetime) and the `Time Filter` enum-property (ie. Last Hour, Last Day, etc.) The value on create for these properties when opening the app should be Time From = Today - 1 week, Time To = Now for the datetime filter properties, and then Last Week for the Time Filter property.
- The `App Variables.Time Filter` property in the UI should have an `On Value Change` event that triggers an action. The action changes the `App Variables.Time From` and `App Variables.Time To` datetimes based on what is the current value of `App Variables.Time Filter` (using conditions and update action nodes).
- And then you can filter your `Tekniske Hendelser`-data source based on the two datetime properties (as you have done).
- To support a custom time range, you would add the` App Variables.Time From` and `App Variables.Time To` as datetime picker components in the UI (as you have done).

The approach with the function in your example is not necessary for this case and would be a more complex approach as you want to filter based on _two_ properties (and you want to be able to select a specific time range as well), and not _one_ as the example your are referring to is using.

For future references; the function did not work _instantly_ because you have not added any function parameters or the Moment library to it (right-hand side where it says **Function Parameters** and _No parameters added_. This can be done in the **Select Parameters** list on the left-hand side. In addition for this specific approach to work, the App Variable would have to be a function type instead of value type.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/appfarm/original/1X/3d67364ff116eb13ca6459ef8c7e9146dc011aed.png)

---

<div class="post-metadata">

**Author:** ![OleSorheim](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/olesorheim/32/1171_2.png) [@OleSorheim](https://community.appfarm.io/u/OleSorheim)\
**Post date:** [June 9, 2023, 12:07pm UTC](https://community.appfarm.io/t/time-filter-for-object-class-data-source/523/3 "2023-06-09T12:07:15Z")

</div>

Thanks for fast reply. The only thing now is how to set the default value of the appvariables to -1 week  
Have tried a number of approaches, though it is unclear what goes wrong. Can i have code in the variable-parameters? And if so, is it in Java?

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/appfarm/original/1X/202b5b7aa9b63fe685a0b4c7e0735dd2fe914c61.png)  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/appfarm/original/1X/f36a7982bef4607da4399e05bac714bf2f59199f.png)

Currently I use a workaround where default value is “null”, with an action “on view load” that sets the value using moment to return “last week” as datetime. However it would be much cleaner if I could set the default value directly

---

<div class="post-metadata">

**Author:** ![Eirik](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/eirik/32/76_2.png) [@Eirik](https://community.appfarm.io/u/Eirik)\
**Post date:** [June 9, 2023, 12:48pm UTC](https://community.appfarm.io/t/time-filter-for-object-class-data-source/523/4 "2023-06-09T12:48:35Z")

</div>

Hi,

To set the `Value on Create` values you click on the three dots and select `Today` or `Now` depending on your use. You can then use the [Value Processor](https://docs.appfarm.io/reference/platform-concepts/value-processor) by clicking the three dots again and select `Value Processor` and create a chain of modifications to the base value.

To get Today - 1 week you would select `Today` as the `Value on Create` and then use the `Value Processor` to subtract 1 week.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/appfarm/original/1X/b82a2ac7ff4cf95970835f35194750685057f961.png)
