# Using Timeframe Selector as an input for a DQL query

**URL:** <https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758>\
**Category:** Developer Q&A Forum\
**Tags:** strato, coding-assistance\
**Created:** [March 9, 2023, 8:30am UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758 "2023-03-09T08:30:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [March 9, 2023, 8:30am UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758/1 "2023-03-09T08:30:31Z")

</div>

Hi,

I’m trying to use a Timeframe Selector ([https://developer.dynatrace.com/reference/design-system/preview/forms/TimeframeSelector/](https://developer.dynatrace.com/reference/design-system/preview/forms/TimeframeSelector/)) for user input for my DQL query. It’s not working as the output of the selector is not compatible with DQL.

Specifically, this is the output of _timeframe.from_ and _timeframe.to:_

![3ene_0-1678350471031.png](https://global.discourse-cdn.com/dynatrace/original/2X/0/05df1725064d71d1df3d2cfd311fe01cdc78dc39.png)

The DQL editor complains about that because ‘now’ is a dynamic variable, and DQL queries only work with static ones. Ergo, only _now()_&nbsp;would work.

Can someone tell me how to do the conversion properly?

Thank you.

---

<div class="post-metadata">

**Author:** ![Wolfi-Stefan](https://avatars.discourse-cdn.com/v4/letter/w/22d042/32.png) [@Wolfi-Stefan](https://community.dynatrace.com/u/Wolfi-Stefan)\
**Post date:** [March 9, 2023, 8:55am UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758/2 "2023-03-09T08:55:45Z")

</div>

Hi!

The onChange event returns the actual timestamp values as well, which you can use in your DQL Queries:  
[https://developer.dynatrace.com/reference/design-system/preview/forms/TimeframeSelector/#get-timeframe-details](https://developer.dynatrace.com/reference/design-system/preview/forms/TimeframeSelector/#get-timeframe-details)

```markup
onChange={(timeframe) => {
   setValue(timeframe);
   setTimeframeDetails(timeframe?.details ?? null);
}}

```

---

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [March 9, 2023, 9:43am UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758/3 "2023-03-09T09:43:54Z")

</div>

Hi Stefan,

that’s cool, but I now run into a different issue: How do I default set TimeframeDetails to now()?

I’d like to run a query immediately on app start and therefore need to have TimeframeDetails already set to now. Is there a way to translate between Timeframe and TimeframeDetails?

---

<div class="post-metadata">

**Author:** ![Wolfi-Stefan](https://avatars.discourse-cdn.com/v4/letter/w/22d042/32.png) [@Wolfi-Stefan](https://community.dynatrace.com/u/Wolfi-Stefan)\
**Post date:** [March 9, 2023, 10:38am UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758/4 "2023-03-09T10:38:17Z")

</div>

You can use the parseTime function, which is also used by the TimeframeSelector internally:&nbsp;[https://developer.dynatrace.com/reference/design-system/preview/core/utils/parseTime/](https://developer.dynatrace.com/reference/design-system/preview/core/utils/parseTime/)

The function returns the TimeDetails type.

---

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [March 9, 2023, 2:59pm UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758/5 "2023-03-09T14:59:15Z")

</div>

I just tried it out and unfortunately it doesn’t work as parseTime returns a TimeDetails and not a TimeframeDetails object. Can you tell me how to create a TimeframeDetails obj?

![3ene_0-1678373899659.png](https://global.discourse-cdn.com/dynatrace/original/2X/d/d6ea1e45e155d94faa429540b9e34f67a7d29198.png)

---

<div class="post-metadata">

**Author:** ![Wolfi-Stefan](https://avatars.discourse-cdn.com/v4/letter/w/22d042/32.png) [@Wolfi-Stefan](https://community.dynatrace.com/u/Wolfi-Stefan)\
**Post date:** [March 9, 2023, 3:52pm UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758/6 "2023-03-09T15:52:37Z")

</div>

The _from_ and _to_ inside of&nbsp;_TimeframeDetails_ are of the type _TimeDetails_. You need to call the parser for both, the from and the to.

---

<div class="post-metadata">

**Author:** ![3ene](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/3ene/32/33646_2.png) [@3ene](https://community.dynatrace.com/u/3ene)\
**Post date:** [March 10, 2023, 3:02pm UTC](https://community.dynatrace.com/t/using-timeframe-selector-as-an-input-for-a-dql-query/38758/7 "2023-03-10T15:02:55Z")

</div>

There were a couple more bumps in the road, so I want to share my final solution to my problem here. Please note that for my app, I needed to initialize the values of the Timeframe and TimeframeDetail variables upon opening the app, which makes this a little more difficult. I do it with following 2 lines:

```auto
const [timeframe, setTimeframe] = React.useState<Timeframe>({from: 'now-7d',to: 'now'});
const [timeframeDetails, setTimeframeDetails] = React.useState<TimeframeDetails>({from: parseTime('now-7d')!, to:parseTime('now')!});

```

Parsing it into DQL works like this for me:

```auto
const queryBase: string = 'fetch events, from: '+JSON.stringify(timeframeDetails?.from.date)+', to:'+JSON.stringify(timeframeDetails?.to.date)

```

This results in following DQL:

```auto
fetch events, from: "2023-03-03T15:01:18.390Z", to:"2023-03-10T15:01:18.390Z" [...]

```

Thanks, Stefan for helping me figure this out!
